This example shows how to manage ActiveMQ Artemis using JMX
ActiveMQ Artemis exposes its managed resources by default on the platform MBeanServer.
To access this MBeanServer remotely, the Java Virtual machine must be started with system properties:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=3000
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
These properties are explained in the Java 5 Management guide (please note that for this example, we will disable user authentication for simplicity sake).
With these properties, ActiveMQ Artemis server will be manageable remotely using standard JMX URL on port 3000
.
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.
ObjectName
corresponding to the queue using a helper class ObjectNameBuilder
ObjectName on = ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queue.getQueueName());
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap());
MBeanServerConnection
from the JMX connector
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
JMSQueueControl
proxy to manage the queue on the server
JMSQueueControl queueControl = (JMSQueueControl)MBeanServerInvocationHandler.newProxyInstance(mbsc,
on,
JMSQueueControl.class,
false);
getMessageCount
method
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
removeMessage
method with the JMS Message ID of the message
System.out.println("message has been removed: " + queueControl.removeMessage(message.getJMSMessageID()));
0
messages
System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
connector.close()
We will now try to consume the message sent to the queue but it won't be there: it has been removed by the management operation
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
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();
}
}