// Step 9. create the JMS management queue.
// It is a "special" queue and it is not looked up from JNDI but constructed directly
Queue managementQueue = HornetQJMSClient.createQueue("hornetq.management");
// Step 10. Create a QueueRequestor for the management queue (see queue-requestor example)
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
// Step 11. Start the Connection to allow the queue requestor to receive replies
connection.start();
// Step 12. Create a JMS message which is used to send a management message
Message m = session.createMessage();
// Step 13. Use a helper class to fill the JMS message with management information:
// * the name of the resource to manage
// * in this case, we want to retrieve the value of the messageCount of the queue
JMSManagementHelper.putAttribute(m, "jms.queue.exampleQueue", "messageCount");
// Step 14. Use the requestor to send the request and wait for the reply
Message reply = requestor.request(m);
// Step 15. Use a helper class to retrieve the operation result
int messageCount = (Integer)JMSManagementHelper.getResult(reply);
System.out.println(queue.getQueueName() + " contains " + messageCount + " messages");
// Step 16. Create another JMS message to use as a management message
m = session.createMessage();
// Step 17. Use a helper class to fill the JMS message with management information:
// * the object name of the resource to manage (i.e. the queue)
// * in this case, we want to call the "removeMessage" operation with the JMS MessageID
// of the message sent to the queue in step 8.
JMSManagementHelper.putOperationInvocation(m,
"jms.queue.exampleQueue",
"removeMessage",
message.getJMSMessageID());
// Step 18 Use the requestor to send the request and wait for the reply
reply = requestor.request(m);
// Step 19. Use a helper class to check that the operation has succeeded
boolean success = JMSManagementHelper.hasOperationSucceeded(reply);
System.out.println("operation invocation has succeeded: " + success);