How to find a horneq Queue length

怎甘沉沦 提交于 2019-11-30 15:26:19

问题


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;

}

回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4877258/how-to-find-a-horneq-queue-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!