如何找到 Horneq 队列长度

发布于 2024-10-15 17:18:52 字数 1462 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫九 2024-10-22 17:18:52

我找到了这两种

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

I found those 2 ways

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

and

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;
}
乖乖 2024-10-22 17:18:52

您必须使用管理界面,本文档末尾是检索消息计数的示例: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

峩卟喜欢 2024-10-22 17:18:52

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

清醇 2024-10-22 17:18:52

脱离上下文但有用,
如果您使用的是 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文