In this tutorial we will cover a basic cluster master-slave configuration of JBoss A-MQ by installing two servers on the same machine. With little changes, you can apply the same configuration to the upstream project, ActiveMQ.
The simplest way to arrange for our cluster will be unzipping A-MQ in two folders, say amq1 and amq2.
First of all, create an administration user, by uncommenting the following line in users.properties
admin=admin,admin,manager,viewer,Operator, Maintainer, Deployer, Auditor, Administrator, SuperUser
Please notice that the configuration is stored in the etc folder for Jboss A-MQ while for ActiveMQ it is in the conf folder
Next, if you are running the two A-MQ on the same machine, you need to change some ports in the amq2 installation, in order to avoid conflicts between the two servers. (If you are using different machines you can skip to “Configuring A-MQ FS Master Slave”).
Open the system.properties file and include an offset (in our case 1000) for the ports exposed:
app1.port=9000 app2.port=9001 app3.port=9002 app1.public.port=9000 app2.public.port=9001 app3.public.port=9002 app1.tls.port=9400 app2.tls.port=9401 app3.tls.port=9402 # # Default port for the OSGI HTTP Service # org.osgi.service.http.port=9181 # # Activemq configuration # activemq.port = 62616 activemq.host = localhost
Configure also the jetty port to be different in the second configuration
<Set name="port"> <Property name="jetty.port" default="9181"/> </Set>
Configuring A-MQ FS Master Slave
There are three different kinds of Master /Slave configuration:
- Shared File system Master/Slave
- JDBC Master/Slave
- Replicated Level DB Master/Slave
We will configre the Shared File system Master/Slave which can be simply set through the activemq.xml as follows:
<persistenceAdapter> <kahaDB directory="/var/mqshared" lockKeepAlivePeriod="5000"/> </persistenceAdapter>
In our case we have set the /var/shared path as shared file system path.
Test your Master-Slave configuration
We will now test the Master-Slave configuration. Start at first the server mq1 from the “bin” folder with:
$ ./start
Now connect with the karaf client:
$ ./client
We will use the Test Producer to send some messages to the failover Protocol.
The failover protocol isolates the consumers and producers from deailing with downtime or reconnection logic when one slave node takes over to become the master which stopped for any reason. In our case the failover url will be: failover://(tcp://localhost:61616,tcp://localhost:62616)
Let’s start sending 10 messages to the Failover URL:
activemq:producer --messageCount 10 --user admin --password admin --brokerUrl "failover://(tcp://localhost:61616,tcp://localhost:62616)" --destination queue://CLUSTER
You can check using the activemq:dstat the current Queue size:
Name Queue Size Producer # Consumer # Enqueue # Dequeue # Forward # CLUSTER 10 0 0 10 0 0
Now start the second A-MQ instance and shutdown the first instance. Send 10 more messages using the failover url:
activemq:producer --messageCount 10 --user admin --password admin --brokerUrl "failover://(tcp://localhost:61616,tcp://localhost:62616)" --destination queue://CLUSTER
Now check the queue size using the activemq:dstat:
Name Queue Size Producer # Consumer # Enqueue # Dequeue # Forward # CLUSTER 20 0 0 10 0 0
You can see that the Queue size contains the total number of JMS messages sent. 10 of these messages have been sent to this server since last restart. Now consume the messages with:
activemq:consumer --messageCount 20 --user admin --password admin --brokerUrl "failover://(tcp://localhost:61616,tcp://localhost:62616)" --destination queue://CLUSTER
As a result, the 20 messages are now included in the dequeue column:
Name Queue Size Producer # Consumer # Enqueue # Dequeue # Forward # CLUSTER 0 0 0 10 20 0