-2

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:

  1. Assume queue name : TESTQUEUE
  2. it has 500 pending messages in queue with JMSCorrelationID
  3. 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() : "");
        }
krutik
  • 119
  • 4
  • 1
    You need to provide more specific details on what you have done, what is happening in the system in the time this takes place etc. – Tim Bish Jul 26 '15 at 11:17
  • I have added the code to retrieve the message. I am able to fetch the message from the ActiveMQ queue when no pending messages. However, It is taking time when attempting fetch a message from pending messages in the same queue. – krutik Jul 27 '15 at 06:01
  • If you have accepted one answer, can you add details about what was wrong and how did you solve your problem? Thanks. – WesternGun Jan 11 '23 at 08:49

1 Answers1

1

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.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42