ActiveMQ - Network of Brokers Explained
Join the DZone community and get the full member experience.
Join For FreeObjective
Why network of brokers?
Scaling enterprise applications on commodity hardware is a rage nowadays. ActiveMQ caters to that very well by being able to create a network of brokers to share the load.
Many times applications running across geographically distributed data centers need to coordinate messages. Running message producers and consumers across geographic regions/data centers can be architected better using network of brokers.
In this 7 part blog series, we will look into the following topics to gain understanding of this very powerful ActiveMQ feature:
- Network Connector Basics - Part 1
- Duplex network connectors - Part 2
- Load balancing consumers on local/remote brokers - Part 3
- Load-balance consumers/subscribers on remote brokers
- Queue: Load balance remote concurrent consumers - Part 4
- Topic: Load Balance Durable Subscriptions on Remote Brokers - Part 5
- Store/Forward messages and consumer failover - Part 6
- How to prevent stuckmessages
- Virtual Destinations - Part 7
- Advanced Messaging with ActiveMQ by Dejan Bosanac [Slides 32-36]
- Understanding ActiveMQ Broker Networks by Jakub Korab
Prerequisites
- ActiveMQ 5.8.0 โ To create broker instances
- Apache Ant โ To run ActiveMQ sample producer and consumers for demo.
Network Connector Basics - Part 1
Setup network connector between broker-1 and broker-2
- Create two broker instances, say broker-1 and broker-2
Ashwinis-MacBook-Pro:bin akuntamukkala$ pwd /Users/akuntamukkala/apache-activemq-5.8.0/bin Ashwinis-MacBook-Pro:bin akuntamukkala$ ./activemq-admin create ../bridge-demo/broker-1 Ashwinis-MacBook-Pro:bin akuntamukkala$ ./activemq-admin create ../bridge-demo/broker-2Since we will be running both brokers on the same machine, let's configure broker-2 such that there are no port conflicts.
- Edit /Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-2/conf/activemq.xml
- Change transport connector to 61626 from 61616
- Change AMQP port from 5672 to 6672 (won't be using it for this blog)
- Edit /Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-2/conf/jetty.xml
- Change web console port to 9161 from 8161
- Configure Network Connector from broker-1 to broker-2
Add the following XML snippet to/Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-1/conf/activemq.xml
<networkConnectors> <networkConnector name="T:broker1->broker2" uri="static:(tcp://localhost:61626)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" dynamicOnly="true"> <excludedDestinations> <queue physicalName=">" /> </excludedDestinations> </networkConnector> <networkConnector name="Q:broker1->broker2" uri="static:(tcp://localhost:61626)" duplex="false" decreaseNetworkConsumerPriority="true" networkTTL="2" dynamicOnly="true"> <excludedDestinations> <topic physicalName=">" /> </excludedDestinations> </networkConnector> </networkConnectors>The above XML snippet configures two network connectors "T:broker1->broker2" (only topics as queues are excluded) and "Q:broker1->broker2" (only queues as topics are excluded). This allows for nice separation between network connectors used for topics and queues.
The name can be arbitrary although I prefer to specify the [type]:[source broker]->[destination broker].
The URI attribute specifies how to connect to broker-2
- Start broker-2
Ashwinis-MacBook-Pro:bin akuntamukkala$ pwd /Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-2/bin Ashwinis-MacBook-Pro:bin akuntamukkala$ ./broker-2 console
- Start broker-1
Ashwinis-MacBook-Pro:bin akuntamukkala$ pwd /Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-1/bin Ashwinis-MacBook-Pro:bin akuntamukkala$ ./broker-1 consoleLogs on broker-1 show 2 network connectors being established with broker-2
INFO | Establishing network connection from vm://broker-1?async=false&network=true to tcp://localhost:61626 INFO | Connector vm://broker-1 Started INFO | Establishing network connection from vm://broker-1?async=false&network=true to tcp://localhost:61626 INFO | Network connection between vm://broker-1#24 and tcp://localhost/127.0.0.1:61626@52132(broker-2) has been established. INFO | Network connection between vm://broker-1#26 and tcp://localhost/127.0.0.1:61626@52133(broker-2) has been established.Web Console on broker-1 @ http://localhost:8161/admin/connections.jsp shows the two network connectors established to broker-2
Let's see this in action
Let's produce 100 persistent messages on a queue called "foo.bar" on broker-1.Ashwinis-MacBook-Pro:example akuntamukkala$ pwd /Users/akuntamukkala/apache-activemq-5.8.0/example Ashwinis-MacBook-Pro:example akuntamukkala$ ant producer -Durl=tcp://localhost:61616 -Dtopic=false -Ddurable=true -Dsubject=foo.bar -Dmax=100
Ashwinis-MacBook-Pro:example akuntamukkala$ ant consumer -Durl=tcp://localhost:61626 -Dtopic=false -Dsubject=foo.bar
Published at DZone with permission of Ashwini Kuntamukkala, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments