Running ActiveMQ in a Hybrid Cloud Environment with Amazon MQ
This post courtesy of Greg Share, AWS Solutions Architect
Many organizations, particularly enterprises, rely on message brokers to connect and coordinate different systems. Message brokers enable distributed applications to communicate with one another, serving as the technological backbone for their IT environment, and ultimately their business services. Applications depend on messaging to work.
In many cases, those organizations have started to build new or “lift and shift” applications to AWS. In some cases, there are applications, such as mainframe systems, too costly to migrate. In these scenarios, those on-premises applications still need to interact with cloud-based components.
Amazon MQ is a managed message broker service for ActiveMQ that enables organizations to send messages between applications in the cloud and on-premises to enable hybrid environments and application modernization. For example, you can invoke AWS Lambda from queues and topics managed by Amazon MQ brokers to integrate legacy systems with serverless architectures. ActiveMQ is an open-source message broker written in Java that is packaged with clients in multiple languages, Java Message Server (JMS) client being one example.
This post shows you can use Amazon MQ to integrate on-premises and cloud environments using the network of brokers feature of ActiveMQ. It provides configuration parameters for a one-way duplex connection for the flow of messages from an on-premises ActiveMQ message broker to Amazon MQ.
ActiveMQ and the network of brokers
First, look at queues within ActiveMQ and then at the network of brokers as a mechanism to distribute messages.
The network of brokers behaves differently from models such as physical networks. The key consideration is that the production (sending) of a message is disconnected from the consumption of that message. Think of the delivery of a parcel: The parcel is sent by the supplier (producer) to the end customer (consumer). The path it took to get there is of little concern to the customer, as long as it receives the package.
The same logic can be applied to the network of brokers. Here’s how you build the flow from a simple message to a queue and build toward a network of brokers. Before you look at setting up a hybrid connection, I discuss how a broker processes messages in a simple scenario.
When a message is sent from a producer to a queue on a broker, the following steps occur:
- A message is sent to a queue from the producer.
- The broker persists this in its store or journal.
- At this point, an acknowledgement (ACK) is sent to the producer from the broker.
When a consumer looks to consume the message from that same queue, the following steps occur:
- The message listener (consumer) calls the broker, which creates a subscription to the queue.
- Messages are fetched from the message store and sent to the consumer.
- The consumer acknowledges that the message has been received before processing it.
- Upon receiving the ACK, the broker sets the message as having been consumed. By default, this deletes it from the queue.
- You can set the consumer to ACK after processing by setting up transaction management or handle it manually using Session.CLIENT_ACKNOWLEDGE.
I now introduce the concept of static propagation with the network of brokers as the mechanism for message transfer from on-premises brokers to Amazon MQ. Static propagation refers to message propagation that occurs in the absence of subscription information. In this case, the objective is to transfer messages arriving at your selected on-premises broker to the Amazon MQ broker for consumption within the cloud environment.
After you configure static propagation with a network of brokers, the following occurs:
- The on-premises broker receives a message from a producer for a specific queue.
- The on-premises broker sends (statically propagates) the message to the Amazon MQ broker.
- The Amazon MQ broker sends an acknowledgement to the on-premises broker, which marks the message as having been consumed.
- Amazon MQ holds the message in its queue ready for consumption.
- A consumer connects to Amazon MQ broker, subscribes to the queue in which the message resides, and receives the message.
- Amazon MQ broker marks the message as having been consumed.
The first step is creating an Amazon MQ broker.
- Sign in to the Amazon MQ console and launch a new Amazon MQ broker.
- Name your broker and choose Next step.
- For Broker instance type, choose your instance size:
- For Deployment mode, enter one of the following:
– Single-instance broker for development and test implementations (recommended)
– Active/standby broker for high availability in production environments
- Scroll down and enter your user name and password.
- Expand Advanced Settings.
- For VPC, Subnet, and Security Group, pick the values for the resources in which your broker will reside.
- For Public Accessibility, choose Yes, as connectivity is internet-based. Another option would be to use private connectivity between your on-premises network and the VPC, an example being an AWS Direct Connect or VPN connection. In that case, you could set Public Accessibility to No.
- For Maintenance, leave the default value, No preference.
- Choose Create Broker. Wait several minutes for the broker to be created.
After creation is complete, you see your broker listed.
For connectivity to work, you must configure the security group where Amazon MQ resides. For this post, I focus on the OpenWire protocol.
For Openwire connectivity, allow port 61617 access for Amazon MQ from your on-premises ActiveMQ broker source IP address. For alternate protocols, see the Amazon MQ broker configuration information for the ports required:
OpenWire – ssl://xxxxxxx.xxx.com:61617
AMQP – amqp+ssl:// xxxxxxx.xxx.com:5671
STOMP – stomp+ssl:// xxxxxxx.xxx.com:61614
MQTT – mqtt+ssl:// xxxxxxx.xxx.com:8883
WSS – wss:// xxxxxxx.xxx.com:61619
Configuring the network of brokers
Configuring the network of brokers with static propagation occurs on the on-premises broker by applying changes to the following file:
<activemq install directory>/conf activemq.xml
This is the first configuration item required to enable a network of brokers. It is only required on the on-premises broker, which initiates and creates the connection with Amazon MQ. This connection, after it’s established, enables the flow of messages in either direction between the on-premises broker and Amazon MQ. The focus of this post is the uni-directional flow of messages from the on-premises broker to Amazon MQ.
The default activemq.xml file does not include the network connector configuration. Add this with the networkConnector element. In this scenario, edit the on-premises broker activemq.xml file to include the following information between <systemUsage> and <transportConnectors>:
<networkConnectors> <networkConnector name="Q:source broker name->target broker name" duplex="false" uri="static:(ssl:// aws mq endpoint:61617)" userName="username" password="password" networkTTL="2" dynamicOnly="false"> <staticallyIncludedDestinations> <queue physicalName="queuename"/> </staticallyIncludedDestinations> <excludedDestinations> <queue physicalName=">" /> </excludedDestinations> </networkConnector> <networkConnectors>
The highlighted components are the most important elements when configuring your on-premises broker.
- name – Name of the network bridge. In this case, it specifies two things:
- That this connection relates to an ActiveMQ queue (Q) as opposed to a topic (T), for reference purposes.
- The source broker and target broker.
- duplex –Setting this to false ensures that messages traverse uni-directionally from the on-premises broker to Amazon MQ.
- uri –Specifies the remote endpoint to which to connect for message transfer. In this case, it is an Openwire endpoint on your Amazon MQ broker. This information could be obtained from the Amazon MQ console or via the API.
- username and password – The same username and password configured when creating the Amazon MQ broker, and used to access the Amazon MQ ActiveMQ console.
- networkTTL – Number of brokers in the network through which messages and subscriptions can pass. Leave this setting at the current value, if it is already included in your broker connection.
- staticallyIncludedDestinations > queue physicalName – The destination ActiveMQ queue for which messages are destined. This is the queue that is propagated from the on-premises broker to the Amazon MQ broker for message consumption.
After the network connector is configured, you must restart the ActiveMQ service on the on-premises broker for the changes to be applied.
Verify the configuration
There are a number of places within the ActiveMQ console of your on-premises and Amazon MQ brokers to browse to verify that the configuration is correct and the connection has been established.
Launch the ActiveMQ console of your on-premises broker and navigate to Network. You should see an active network bridge similar to the following:
This identifies that the connection between your on-premises broker and your Amazon MQ broker is up and running.
Now navigate to Connections and scroll to the bottom of the page. Under the Network Connectors subsection, you should see a connector labeled with the name: value that you provided within the ActiveMQ.xml configuration file. You should see an entry similar to:
Amazon MQ broker
Launch the ActiveMQ console of your Amazon MQ broker and navigate to Connections. Scroll to the Connections openwire subsection and you should see a connection specified that references the name: value that you provided within the ActiveMQ.xml configuration file. You should see an entry similar to:
If you configured the uri: for AMQP, STOMP, MQTT, or WSS as opposed to Openwire, you would see this connection under the corresponding section of the Connections page.
Testing your message flow
The setup described outlines a way for messages produced on premises to be propagated to the cloud for consumption in the cloud. This section provides steps on verifying the message flow.
Verify that the queue has been created
After you specify this queue name as staticallyIncludedDestinations > queue physicalName: and your ActiveMQ service starts, you see the following on your on-premises ActiveMQ console Queues page.
As you can see, no messages have been sent but you have one consumer listed. If you then choose Active Consumers under the Views column, you see Active Consumers for TestingQ.
This is telling you that your Amazon MQ broker is a consumer of your on-premises broker for the testing queue.
Produce and send a message to the on-premises broker
Now, produce a message on an on-premises producer and send it to your on-premises broker to a queue named TestingQ. If you navigate back to the queues page of your on-premises ActiveMQ console, you see that the messages enqueued and messages dequeued column count for your TestingQ queue have changed:
What this means is that the message originating from the on-premises producer has traversed the on-premises broker and propagated immediately to the Amazon MQ broker. At this point, the message is no longer available for consumption from the on-premises broker.
If you access the ActiveMQ console of your Amazon MQ broker and navigate to the Queues page, you see the following for the TestingQ queue:
This means that the message originally sent to your on-premises broker has traversed the network of brokers unidirectional network bridge, and is ready to be consumed from your Amazon MQ broker. The indicator is the Number of Pending Messages column.
Consume the message from an Amazon MQ broker
Connect to the Amazon MQ TestingQ queue from a consumer within the AWS Cloud environment for message consumption. Log on to the ActiveMQ console of your Amazon MQ broker and navigate to the Queue page:
As you can see, the Number of Pending Messages column figure has changed to 0 as that message has been consumed.
This diagram outlines the message lifecycle from the on-premises producer to the on-premises broker, traversing the hybrid connection between the on-premises broker and Amazon MQ, and finally consumption within the AWS Cloud.
This post focused on an ActiveMQ-specific scenario for transferring messages within an ActiveMQ queue from an on-premises broker to Amazon MQ.
For other on-premises brokers, such as IBM MQ, another approach would be to run ActiveMQ on-premises broker and use JMS bridging to IBM MQ, while using the approach in this post to forward to Amazon MQ. Yet another approach would be to use Apache Camel for more sophisticated routing.
I hope that you have found this example of hybrid messaging between an on-premises environment and the AWS Cloud to be useful. Many customers are already using on-premises ActiveMQ brokers, and this is a great use case to enable hybrid cloud scenarios.
To learn more, see the Amazon MQ website and Developer Guide. You can try Amazon MQ for free with the AWS Free Tier, which includes up to 750 hours of a single-instance mq.t2.micro broker and up to 1 GB of storage per month for one year.