To run the example, simply type mvn verify -Djavax.net.ssl.keyStore=target/server0/etc/activemq.example.keystore -Djavax.net.ssl.keyStorePassword=activemqexample -Djavax.net.ssl.trustStore=target/server0/etc/activemq.example.truststore -Djavax.net.ssl.trustStorePassword=activemqexample from this directory,
or add -PnoServer if you want to start and create the server manually.
This example shows how to manage ActiveMQ Artemis using JMX using SSL
ActiveMQ Artemis exposes its managed resources by default on the platform MBeanServer.
To access this MBeanServer remotely, add the following to the management.xml configuration:
With these properties, ActiveMQ Artemis server will be manageable remotely using standard JMX URL on port 1099
.
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();
}
}