ActiveMQ - Network of Brokers Explained (Part Four)
Join the DZone community and get the full member experience.
Join For FreeIn the previous part 3, we have seen how ActiveMQ helps distinguish remote consumers from local consumers which helps in determining shorter routes from message producers to consumers. In this part 4, we will look into how to load balance concurrent consumers on remote brokers.
![]() |
Part 4 - Network of brokers |
Let's see this in action
- Ashwinis-MacBook-Pro:bin akuntamukkala$ pwd
/Users/akuntamukkala/apache-activemq-5.8.0/bin - Ashwinis-MacBook-Pro:bin akuntamukkala$./activemq-admin create ../cluster/broker-1
- Ashwinis-MacBook-Pro:bin akuntamukkala$./activemq-admin create ../cluster/broker-2
- Ashwinis-MacBook-Pro:bin akuntamukkala$./activemq-admin create ../cluster/broker-3
- Fix the broker-2 and broker-3 transport, amqp connectors and jetty http port by modifying the corresponding conf/activemq.xml and conf/jetty.xml as follows:
Broker Openwire Port Jetty HTTP Port AMQP Port broker-1 61616 8161 5672 broker-2 61626 9161 5682 broker-3 61636 10161 5692 - Fix network connector on broker-1 such that messages on queues can be forwarded dynamically to consumers on broker-2 and broker-3. This can be done by adding the following XML snippet into broker-1's conf/activemq.xml
<networkConnectors> <networkConnector name="Q:broker1->broker2" uri="static:(tcp://localhost:61626)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" dynamicOnly="true"> <excludedDestinations> <topic physicalName=">" /> </excludedDestinations> </networkConnector> <networkConnector name="Q:broker1->broker3" uri="static:(tcp://localhost:61636)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" dynamicOnly="true"> <excludedDestinations> <topic physicalName=">" /> </excludedDestinations </networkConnector> </networkConnectors>
- Start broker-2, broker-3 and broker-1. We can start these in any order.
- /apache-activemq-5.8.0/cluster/broker-3/bin$ ./broker-3 console
- /apache-activemq-5.8.0/cluster/broker-2/bin$ ./broker-2 console
- /apache-activemq-5.8.0/cluster/broker-1/bin$ ./broker-1 console
- Let's start the consumers C1 on broker-2 and C2, C3 on broker-3 but on the same queue called "moo.bar"
- /apache-activemq-5.8.0/example$ ant consumer -Durl=tcp://localhost:61626 -Dsubject=moo.bar
- /apache-activemq-5.8.0/example$ ant consumer -Durl=tcp://localhost:61636 -Dsubject=moo.bar -DparallelThreads=2
The consumer subscriptions are forwarded by broker-2 and broker-3 to their neighboring broker-1 which has a network connector established to both broker-2 and broker-3 by the use of advisory messages. - Let's review the broker web consoles to see the queues and corresponding consumers.
- We find that broker-2's web console shows one queue "moo.bar" having 1 consumer, broker-3's web console shows one queue "moo.bar" having 2 concurrent consumers
- Though there are three consumers (C1 on broker-2 and C2,C3 on broker-3), broker-1 sees only two consumers (representing broker-2 and broker-3).
This is because the network connector from broker-1 to broker-2 and to broker-3 by default has a property "conduitSubscriptions" which is true.
Due to which broker-3's C2 and C3 which consume messages from the same queue "moo.bar" are treated as one consumer in broker-1.- Let's produce 30 messages into broker-1's queue moo.bar and see how the messages are divvied among the consumers C1, C2 and C3
![]() |
http://localhost:8161/admin/queues.jsp |
![]() |
Shows how the messages were propagated from producer to consumers C1, C2, C3 |
<networkConnectors> <networkConnector name="Q:broker1->broker2" uri="static:(tcp://localhost:61626)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" conduitSubscriptions="false" dynamicOnly="true"> <excludedDestinations> <topic physicalName=">" /> </excludedDestinations> </networkConnector> <networkConnector name="Q:broker1->broker3" uri="static:(tcp://localhost:61636)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" conduitSubscriptions="false" dynamicOnly="true"> <excludedDestinations> <topic physicalName=">" /> </excludedDestinations> </networkConnector> </networkConnectors>
![]() |
Shows how the messages were propagated from producer to consumers C1, C2, C3 |
As always, your comments and feedback is appreciated!
In the next part 5, we will explore how the same scenario will play out if we were to use a topic instead of a queue. Stay tuned...
References
Resources
- The configuration files (activemq.xml and jetty.xml) of all the brokers used in this blog are available here.
Published at DZone with permission of Ashwini Kuntamukkala, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments