如何找到horneq队列长度 [英] How to find a horneq Queue length
本文介绍了如何找到horneq队列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Hornetq 2.0我不明白我怎么能知道目前有多少消息在队列中。
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.
我没有使用JMS api,而是使用高度优化的核心API。
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 ?
我找到2路但不知道是什么正确的方法。
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;
}
或
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;
}
推荐答案
我找到了这两种方式
public synchronized 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;
}
这篇关于如何找到horneq队列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文