如何在Artemis获得队列大小(深度)

时间:2018-03-11 22:05:42

标签: jms activemq-artemis

使用Java客户端API,如何在远程ActiveMQ Artemis 2.4.0 代理中获得队列大小

2 个答案:

答案 0 :(得分:0)

stats等功能属于管理API。它可以通过消息传递API通过HTTP(jolokia),JMX或JMX进行访问。

官方documentation中有一个例子,用户可以持久阅读它。 为方便起见,我会在这里背诵。

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");

答案 1 :(得分:0)

这是完整的可运行示例:

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);