Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ActiveMQ - Network of Brokers Explained (Part Two)

DZone's Guide to

ActiveMQ - Network of Brokers Explained (Part Two)

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

In this blog we will see how  duplex network connectors work.
In the previous  part 1 we created a network connector from broker-1 and broker-2. We were able to see how messages for queue "foo.bar" on broker-1 were forwarded queue "foo.bar" on broker-2 when there was a consumer on broker-2 for queue "foo.bar"
Let's try doing the reverse by producing messages into broker-2's queue foo.bar and consume from broker-1's queue "foo.bar"
Ashwinis-MacBook-Pro:example akuntamukkala$ ant producer -Durl=tcp://localhost:61626 -Dtopic=false -Ddurable=true -Dsubject=foo.bar -Dmax=100

Ashwinis-MacBook-Pro:example akuntamukkala$ ant consumer -Durl=tcp://localhost:61616 -Dtopic=false -Dsubject=foo.bar
In the previous blog post, we had enqueued/dequeued 100 messages. Hence the #messages enqueued now shows as 200 here. 
As shown above, 100 new messages are enqueued on foo.bar queue on broker-2 but there are no consumers though there is a network connector for all queues from broker-1 to broker-2. 
The reason is that a network connector unless specified as "duplex" is unidirectional from the source to the destination broker. 
Let's change the following attribute highlighted in yellow in /Users/akuntamukkala/apache-activemq-5.8.0/bridge-demo/broker-1/conf/activemq.xml configuration file for broker-1.
     <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="true" 
            decreaseNetworkConsumerPriority="true" 
            networkTTL="2" 
            dynamicOnly="true">
            <excludedDestinations>
                  <topic physicalName=">" />
            </excludedDestinations>
         </networkConnector>
     </networkConnectors>
Let's restart the brokers and connect to the brokers using jConsole.
Here is broker-1 jConsole MBean tab screenshot which shows the following:
  1. Q:broker1->broker2 network connector is duplex.
  2. There is now a dynamic producer into broker-1 from broker-2 because the
    Q:broker1->broker2 network connector is "duplex".
Here is broker-2 jConsole MBean tab screenshot which shows the following:
  1. Duplex network connector from broker-2 to broker-1
  2. Two dynamic message producers from broker-1 to broker-2
    1. Please note that "Q:broker1->broker2" network connector shows as duplex as configured in activemq.xml 

Let's see this in action

  1. Producer 100 messages into broker-2
Ashwinis-MacBook-Pro:example akuntamukkala$ ant producer -Durl=tcp://localhost:61626 -Dtopic=false -Ddurable=true -Dsubject=foo.bar -Dmax=100
Screenshot of queues in broker-2: http://localhost:9161/admin/queues.jsp
    2.  Create a consumer on foo.bar on broker-1
Ashwinis-MacBook-Pro:example akuntamukkala$ ant consumer -Durl=tcp://localhost:61616 -Dtopic=false -Dsubject=foo.bar
The following screenshot from broker-2 shows that all the 100 messages have been dequeued by a consumer (dynamically forwarded to broker-1).
http://localhost:9161/admin/queues.jsp
The following screenshot shows the details of this dynamic consumer on broker-2's foo.bar queue.
http://localhost:9161/admin/queueConsumers.jsp?JMSDestination=foo.bar
The following screenshot shows that the 100 messages which were dynamically moved from broker-2's foo.bar queue to broker-1's foo.bar queue have been successfully consumed by the consumer which we created in step #2
This concludes part 2 of this series where we saw how duplex network connectors work. 
As always your comments are very welcome. 
Stay tuned for part 3 where we will go over load balancing consumers on local/remote brokers...

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:

Published at DZone with permission of Ashwini Kuntamukkala, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}