Apache ActiveMQ is a high performing message broker, however if clustering is needed, it supports a number of methods. Out of the latter mentioned methods the Master/Slave is a pattern where the persistence layer is shared between multiple broker instances. A single
Master broker connects to the persistence; while, the rest of the
Slave brokers keep waiting to attain the lock, which is based on the persistence. If the Master node goes down, the lock for the persistence is released and a Slave quickly acquires it allowing a client to continue operations without any data loss. The clients should connect to the Master/Slave setup using the
failover: transport, or they should implement a manual failover mechanism to automatically connect to the next available broker when the first one goes down.
Setting up a Master/Slave
The following instructions shows you how to setup two broker instances in the same machine. The two instances will open different ports for the protocols, so that there will be no conflicts. The instances will use the flat file based embedded KahaDB as the persistence layer and the two instances will share the KahaDB instance.
Step 1 - Create two broker instances
- Download ActiveMQ and unzip the ActiveMQ distribution in both the instances.
Offset port values in the second ActiveMQ distribution, so that different ports can be used and that there will be no conflicts with the ports used by the different protocol connectors.
Change the port offset in the
Point the KahaDB persistence to the same location in both the instances.
This will ensure that only one instance at a time is able to acquire the lock to the DB and when the lock is released, the other instance will be able get it from the same location.
persistenceAdaptertag inside the
<ACTIVEMQ_HOME>/conf/activemq.xmlfile as follows for this purpose.
- Connect the instances by making the
networkConnectorin each of the instances point to each other.
Add the following block into the
<ACTIVEMQ_HOME>/conf/activemq.xmlfile, after the
persistenceAdapterblock, in the Master pointing to the Slave's OpenWire port.
61626is the OpenWire port in the Slave instance.
Add the following block into the
<ACTIVEMQ_HOME>/conf/activemq.xmlfile after the
persistenceAdapterblock in the Slave pointing to the Master’s OpenWire port.
Apache ActiveMQ uses static discovery to statically point to the existing broker instances.
Step 2 - Start the instances
Start the Message Broker as follows:
When observing the logs, you will see some log entries similar to the following repeatedly appearing. The latter mentioned log entries appear because the Master broker will initially look for the Slave broker when it starts up.
Start the Slave broker and tail the logs.
You will see a different set of logs appearing.
The above log entry appears because the lock for the shared DB is already acquired by the Master broker. The Slave broker will not start until it is able to acquire the lock for the DB. If you try to check as to which ports are open using the
netstatcommand, you will see that only the Master broker is up and running and ready to accept requests.
Now if you connect to the broker setup using the failover: transport, you will see that the client is connected the Master broker.
Create a queue and publish an event to the queue without consuming it.
Now stop the Master broker.
You will see the Slave broker acquiring the lock to the DB and becoming ready to accept requests.
Start a consumer with the failover transport, and observe it connecting to and retrieving the event (which was published to the Master broker) from the Slave broker.
You will see that there was no data loss and the service did not stop responding for more than a few moments, which was the time the Slave took to start up after acquiring the DB lock.