Configure Network of Brokers Queue Consumer To Never Be Localhost Unless Broker Failure
-
13-12-2019 - |
Question
I have a pair of Brokers configured as a network of brokers with ActiveMQ 5.5.1. Each broker exists on a server that communicates across a WAN.
BrokerA on ServerA
<amq:broker useJmx="false" persistent="false">
<amq:networkConnectors>
<amq:networkConnector uri="static:(tcp://BrokerB:61616)" duplex="true" />
</amq:networkConnectors>
<amq:transportConnectors>
<amq:transportConnector uri="tcp://BrokerA:61616" />
</amq:transportConnectors>
</amq:broker>
BrokerB on ServerB
<amq:broker useJmx="false" persistent="false">
<amq:networkConnectors>
<amq:networkConnector uri="static:(tcp://BrokerA:61616)" duplex="true" />
</amq:networkConnectors>
<amq:transportConnectors>
<amq:transportConnector uri="tcp://BrokerB:61616" />
</amq:transportConnectors>
</amq:broker>
On each server, I have a Java service running that creates the following topic and queue:
<amq:queue id="myQueue" physicalName="myQueue" />
<amq:topic id="myTopic" physicalName="MyTopic" />
The java service (on each server) creates a producer and a consumer for myQueue as well as a producer and consumer for myTopic. I want both consumers on myTopic to receive all topic messages, which it does. The problem lies with myQueue. I only want the configured broker to consume myQueue messages. For example, if BrokerA produces a service message on ServerA, I only want BrokerB on ServerB to consume that message. Right now in my test I am seeing about 50% of messages produced on ServerA consumed by BrokerA and 50% consumed by BrokerB. In addition, if a BrokerB goes offline, BrokerA must accept all myQueue messages from both ServerA and ServerB and consume all myQueue messages until BrokerB comes back online.
I had thought about using message groups, but I couldn't come up with a good way to detect when failure has occurred to switch which group a message is sent to. Any recommendations would be greatly appreciated!
Solution
you can specify 'excludedDestinations' to prevent a queue's messages from going to other brokers...
<networkConnectors>
<networkConnector uri="static:(tcp://BrokerB:61616)" duplex="true" >
<excludedDestinations>
<queue physicalName="myQueue"/>
</excludedDestinations>
</networkConnector>
</networkConnectors>
to your other question about making sure the consumer 'never connects to local broker unless broker failure'...if you do the following in your client connection, you'll always connect to a remote broker unless its unavailable...also, you'll recover to the remote broker when it becomes available
failover:(tcp://remoteBroker,tcp://localBroker)?randomize=false&priorityBackup=true