How to Forward messages from one ActiveMQ to another
Apache ActiveMQ is an open source message broker written in Java together with a full Java Message Service (JMS) client. It provides "Enterprise Features" which in this case means fostering the communication from more than one client or server.
In the enterprise, ActiveMQ is celebrated for its flexibility in configuration, and its support for a relatively large number of transport protocols, including OpenWire, STOMP, MQTT, AMQP, REST, and WebSockets.
ActiveMQ uses transport connectors over which it communicates with message producers and consumers. However, in order to facilitate broker to broker communication, ActiveMQ uses network connectors.
A network connector is a bridge between two brokers which allows on-demand message forwarding.
In other words, if Broker B1 initiates a network connector to Broker B2 then the messages on a channel (queue/topic) on B1 get forwarded to B2 if there is at least one consumer on B2 for the same channel. If the network connector was configured to be duplex, the messages get forwarded from B2 to B1 on demand.
Prerequisites
- ActiveMQ 5.15.2 – To create broker instances (download from here)
The following diagram shows how a network connector functions. It bridges two brokers and is used to forward messages from Broker-1 to Broker-2 on demand if established by Broker-1 to Broker-2.
Setup network connector between broker-1 and broker-2
Step 1: Create bridge-demo folder under downloaded apache-activemq-5.15.2 folder and create two broker instances, say broker 1 and broker 2 folder inside bridge-demo folder.
Step 2: Copy everything from downloaded apache-activemq-5.15.2 folder and put in broker 1 and broker 2 folder.
Step 3: Since we will be running both brokers on the same machine, let's configure broker-2 such that there are no port conflicts.
Edit \apache-activemq-5.15.2\bridge-demo\broker-2\conf\activemq.xml as below.
- Add brokerName as broker-2. <broker xmlns="http://activemq.apache.org/schema/core" brokerName="broker-2" dataDirectory="${activemq.data}">
- Edit transportConnectors tag as
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61626?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:6672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61623?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
Edit \apache-activemq-5.15.2\bridge-demo\broker-2\conf\jetty.xml as below.
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="9161"/>
</bean>
Step 4: Configure Network Connector from broker-1 to broker-2 Add the following XML snippet to \apache-activemq-5.15.2\bridge-demo\broker-1\conf\activemq.xml
<networkConnectors>
<networkConnector
name="Q:broker1->broker2"
uri="static:(tcp://localhost:61626)"
userName="admin"
password="admin"
duplex="false"
decreaseNetworkConsumerPriority="true"
networkTTL="3">
<staticallyIncludedDestinations>
<queue physicalName="test"/>
</staticallyIncludedDestinations>
</networkConnector>
</networkConnectors>
Step 5: Based on your system configuration go to win32 or win64 folder of broker-2 and click on activemq.bat
webconsole for broker-2 available on http://localhost:9161/
Step 6: Go to broker-1 folder of win32 or win64 and click on activemq.bat
webconsole for broker-1 available on http://localhost:8161/
You can check network connectors on Connectios and Network tab on web console of broker-1. Username and password would be admin.
You can check connector openwire on Connectios tab on web console of broker-2. Username and password would be admin.
Step 7: Add gem stomp in Gemfile. gem 'stomp'
Step 8: Create publisher.rb file and add below code.
require 'rubygems'require 'stomp' client = Stomp::Client.new("admin","admin","localhost",61613,true) client.publish "/queue/test", "Hello world", { :persistent => true } client.close
Step 9: Run publisher.rb
Step 10: Create consumer.rb file and add below code.
require 'rubygems'require 'stomp' client = Stomp::Client.new("admin","admin","localhost",61623,true)client.subscribe("/queue/test") do | message | puts messageendclient.join