问题
Using a Java client API, how can I get the queue size in a remote ActiveMQ Artemis 2.4.0 broker?
回答1:
Such feature as stats belongs to the management API. It can be accessed over HTTP (jolokia), JMX or JMX via the messaging API.
There is an example in the official documentation for users persistent enough to read it. I will recite it here for convenience.
ClientSession session = ...
ClientRequestor requestor = new ClientRequestor(session, "activemq.management");
ClientMessage message = session.createMessage(false);
ManagementHelper.putAttribute(message, "queue.exampleQueue", "messageCount");
session.start();
ClientMessage reply = requestor.request(m);
int count = (Integer) ManagementHelper.getResult(reply);
System.out.println("There are " + count + " messages in exampleQueue");
回答2:
This is the complete runnable example:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientRequestor;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.jms.client.ActiveMQSession;
String queueName = "MY.QUEUE";
Hashtable<String, String> env = new Hashtable<>();
env.put("java.naming.factory.initial", "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
env.put("java.naming.provider.url", "tcp://localhost:61616");
InitialContext initialContext = new InitialContext(env);
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("ConnectionFactory");
Connection connection = cf.createConnection();
Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession session = ((ActiveMQSession)jmsSession).getCoreSession();
ClientRequestor requestor = new ClientRequestor(session, "activemq.management");
session.start();
ClientMessage message = session.createMessage(false);
ManagementHelper.putAttribute(message, "queue." + queueName, "messageCount");
ClientMessage reply = requestor.request(message);
long count = (Long)ManagementHelper.getResult(reply);
System.out.println("There are " + count + " messages in " + queueName);
来源:https://stackoverflow.com/questions/49225634/how-to-get-queue-size-depth-in-artemis