如何找到 Horneq 队列长度
我正在使用 Hornetq 2.0,我不明白如何知道目前队列中有多少消息。
这是一个非常有用的功能,因此我可以在运行时知道我的消费者是否足够快地消费消息。
我没有使用 JMS api,而是使用高度优化的核心 API。
获取队列中消息数量的正确(最快)方法是什么?
我找到了两种方法,但不知道什么是正确的方法。
public int size(){
ClientSession session;
try {
session = sf.createSession(false, false, false);
ClientRequestor requestor = new ClientRequestor(session, "hornetq.management");
ClientMessage m = session.createMessage(false);
ManagementHelper.putAttribute(m, "core.queue." + queueName, "messageCount");
ClientMessage reply = requestor.request(m);
int count = (Integer) ManagementHelper.getResult(reply);
return count;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
或者
public synchronized int size(){
ClientSession coreSession = null;
int count = 0;
try {
coreSession = sf.createSession(false, false, false);
ClientSession.QueueQuery result;
result = coreSession.queueQuery(new SimpleString(queueName));
count = result.getMessageCount();
} catch (HornetQException e) {
e.printStackTrace();
} finally {
if (coreSession!= null ){
try {
coreSession.close();
} catch (HornetQException e) {
e.printStackTrace();
}
}
}
return count;
}
I am using Hornetq 2.0 i dont understand how can i know how many message are sitting on the queue at the moment.
This is a very useful feature so i can know at runtime if my consumer consume message fast enough.
I am not using the JMS api but the highly optimised core API.
What is the right (fastest) way to get the number of message in the queue ?
I found 2 way but don't know what is the proper way to do it.
public int size(){
ClientSession session;
try {
session = sf.createSession(false, false, false);
ClientRequestor requestor = new ClientRequestor(session, "hornetq.management");
ClientMessage m = session.createMessage(false);
ManagementHelper.putAttribute(m, "core.queue." + queueName, "messageCount");
ClientMessage reply = requestor.request(m);
int count = (Integer) ManagementHelper.getResult(reply);
return count;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
or
public synchronized int size(){
ClientSession coreSession = null;
int count = 0;
try {
coreSession = sf.createSession(false, false, false);
ClientSession.QueueQuery result;
result = coreSession.queueQuery(new SimpleString(queueName));
count = result.getMessageCount();
} catch (HornetQException e) {
e.printStackTrace();
} finally {
if (coreSession!= null ){
try {
coreSession.close();
} catch (HornetQException e) {
e.printStackTrace();
}
}
}
return count;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了这两种
方法
I found those 2 ways
and
您必须使用管理界面,本文档末尾是检索消息计数的示例:http://hornetq.sourceforge.net/docs/hornetq-2.0.0.GA/user-manual/en/html/management .html#management.message-counters
You have to use the management interface, at the end of this document is an example of retrieving message counts: http://hornetq.sourceforge.net/docs/hornetq-2.0.0.GA/user-manual/en/html/management.html#management.message-counters
hornetq-2.2.14.Final 附带了一个关于消息计数器的示例。它位于 hornetq-2.2.14.Final/examples/jms/message-counters
hornetq-2.2.14.Final ships with an example on message-counters. It's located at hornetq-2.2.14.Final/examples/jms/message-counters
脱离上下文但有用,
如果您使用的是 jboss,那么您可以使用下面的 cli 命令来获取队列消息计数。
/subsystem=messaging/hornetq-server=default/jms-queue=:read-attribute(name=message-count)
Out of context but useful,
If you're using jboss then you can use below cli command to get queue message count.
/subsystem=messaging/hornetq-server=default/jms-queue=:read-attribute(name=message-count)