Q:

Performance Issue while fetching message from pending message list of a queue

Queue Server: ActiveMQ,
Protocol: AMQP
API: Apache QPID client JMS 0.3.0
In normal case, It is working fine for me and fetch the message in few milliseconds.
Facing issue in below scenario:

Assume queue name : TESTQUEUE
it has 500 pending messages in queue with JMSCorrelationID
Fetching a message with JMSCorrelationID using qpid API.

It takes around min 25 sec to retrieve the message.
What should I do in this case?
    // LOGIC: declaration
    Connection connection = null;
    MessageConsumer consumer = null;
    MetaData objMetaData = new MetaData();
    String strReturnData = "";
    String user = "";
    String password = "";
    String host = "";
    int port = 0;
    ConnectionFactoryImpl factory = null;
    Session session = null;
    Destination destination = null;
    Message msg = null;

    try {

        // LOGIC: set the connection details
        user = objMetaData.getMetaData(CommonConstant.QUEUE_USERNAME);
        password = objMetaData.getMetaData(CommonConstant.QUEUE_PASSWORD);
        host = objMetaData.getMetaData(CommonConstant.QUEUE_HOST);
        port = Integer.parseInt(objMetaData.getMetaData(
                CommonConstant.QUEUE_PORT).trim());

        // LOGIC: Initialize the connection factory with connection details
        factory = new ConnectionFactoryImpl(host, port, user, password, strCorelationId);

        // LOGIC: create connection
        connection = factory.createConnection();

        // LOGIC: create session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // LOGIC: initialize destination with queue name
        destination = (Destination) session.createQueue(strQueueName);

        // LOGIC: open connection
        connection.start();

        // LOGIC: set correlation id
        consumer = session
                .createConsumer(
                        destination,"JMSCorrelationID ='"+ strCorelationId + "'");

        // LOGIC: get message
        if (blnIsRequestModeSync) {
            msg = consumer.receive((1000 * 60 * 2));
        } else {
            msg = consumer.receive(30000);
        }

        // LOGIC: check message type
        if (msg instanceof TextMessage) {

            // LOGIC: when text message
            TextMessage txtmsg = (TextMessage) msg;
            //System.out.println("TEXT MSG: " + txtmsg.getText());
            strReturnData = txtmsg.getText();
        } else {
            // LOGIC: when object message
            ObjectMessage objmsg = (ObjectMessage) msg;
            //System.out.println("Object  MSG:" + objmsg);
            strReturnData = (null != objmsg ? objmsg.toString() : "");
        }

A:

There isn't enough information to give you a definitive answer but there are a couple things that could be at play here.
The first is that you have multiple consumers on the Queue and the messages are being prefetched to them based on when they arrive such that the first consumer is holding onto messages that are of interest to the second etc, etc.  You could try reducing prefetch on the client to rule that out.
The second is that the Queue depth is large enough that the message your client is interested in is deeper into the Queue that the default page size that ActiveMQ uses to hold messages in memory which means that the client wouldn't receive its message until some other consumer pulls off enough messages to make the one your slow client is interested available in memory for selection.  You can adjust the page size that ActiveMQ uses on a per-destination basis.  However you should consider yourself warned that selectors over deep queues is a messaging anti-pattern and is prone to these sorts of troubles.  

