This example shows how to manage ActiveMQ using JMS Messages to invoke management operations on the server.
To manage ActiveMQ using JMX, see the JMX example.
ActiveMQ can be managed by sending JMS messages with specific properties to its management queue.
By default, the management name is calledactivemq.management
but this can be configured in activemq-configuration.xml
<management-address>activemq.management</management-address>
The management queue requires a "special" user permission manage
to be able to receive management messages.
This is also configured in activemq-configuration.xml
<security-setting match="activemq.management">
<permission type="manage" roles="guest" />
</security-setting>
To run the example, simply type mvn verify -Pexample
from this directory
InitialContext initialContext = getContext(0);
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(topic);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
Now that we have a message in the queue, we will manage the queue by retrieving the number of messages in the queue (i.e. 1) and by removing the message which has been sent in step 8.
Queue managementQueue = new ActiveMQQueue("activemq.management", "activemq.management");
QueueRequestor
to send messages to the management queue and receive replies (see queue-requestor example)
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
connection.start()
Message m = session.createMessage();
JMSManagementHelper
to fill these properties:
jms.queue.exampleQueue
(i.e. jms.queue
followed by the name of the queue as defined in activemq-jms.xml)MessageCount
JMSManagementHelper.putAttribute(m, "jms.queue.exampleQueue", "MessageCount");
Message reply = requestor.request(m);
JMSManagementHelper
to retrieve the result from the reply message:
int messageCount = (Integer)JMSManagementHelper.getResult(reply);
System.out.println(queue.getQueueName() + " contains " + messageCount + " messages");
m = session.createMessage();
jms.queue.exampleQueue
removeMessage
JMSManagementHelper.putOperationInvocation(m, "jms.queue.exampleQueue", "removeMessage", message.getJMSMessageID());
reply = requestor.request(m);
boolean success = JMSManagementHelper.hasOperationSucceeded(reply);
System.out.println("operation invocation has succeeded: " + success);
JMSManagementHelper
to retrieve the result from the reply message:
(in our case, the removeMessage
method returns a boolean)
boolean messageRemoved = (Boolean)JMSManagementHelper.getResult(reply);
System.out.println("message has been removed: " + messageRemoved);
We will now consume the message from the queue but there will be none: the message sent at step 8 was removed by the management operation
MessageConsumer messageConsumer = session.createConsumer(queue);
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}