6.3. Managing AMQ Broker using the JMS API
The Java Message Service (JMS) API allows you to create, send, receive, and read messages. You can use JMS and the Red Hat build of Apache Qpid JMS client to manage brokers.
To use JMS to manage a broker, you must first configure the broker’s management address with the manage permission.
Procedure
-
Open the
<broker_instance_dir>/etc/broker.xmlconfiguration file. Add the
<management-address>element, and specify a management address.By default, the management address is
activemq.management. You only need to specify a different address if you do not want to use the default.<management-address>my.management.address</management-address>Provide the management address with the
manageuser permission type.This permission type enables the management address to receive and handle management messages.
<security-setting-match="activemq.management"> <permission-type="manage" roles="admin"/> </security-setting>
To invoke management operations using JMS messages, the Red Hat build of Apache Qpid JMS client must instantiate the special management queue.
Procedure
-
Create a
QueueRequestorto send messages to the management address and receive replies. -
Create a
Message. -
Use the helper class
org.apache.activemq.artemis.api.jms.management.JMSManagementHelperto fill the message with the management properties. -
Send the message using the
QueueRequestor. -
Use the helper class
org.apache.activemq.artemis.api.jms.management.JMSManagementHelperto retrieve the operation result from the management reply.
例 6.2. Viewing the number of messages in a queue
This example shows how to use the JMS API to view the number of messages in the JMS queue exampleQueue:
Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
QueueSession session = ...
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
connection.start();
Message message = session.createMessage();
JMSManagementHelper.putAttribute(message, "queue.exampleQueue", "messageCount");
Message reply = requestor.request(message);
int count = (Integer)JMSManagementHelper.getResult(reply);
System.out.println("There are " + count + " messages in exampleQueue");