简体   繁体   中英

How to search for a particular message in JMS queue

I am sending some messages to a JMS queue. What are the possible ways to search for a particular message in a queue to consume?

I tried out in the following way: I am setting the JMSCorrelationID while sending a message to the queue:

 public void createDQueue(String queuename, String json, Integer userid) {
          try {

                QueueSession.AUTO_ACKNOWLEDGE );
                Queue queue = session.createQueue(queuename);
                ObjectMessage objectMessage = session.createObjectMessage();
                objectMessage.setJMSCorrelationID(String.valueOf(userid));
                objectMessage.setObject(json);
                session.createSender(queue).send(objectMessage);
                session.close();
                connection.close();
          }catch(Exception e){
              e.printStackTrace();
          }
      }

In the consumer code I want to get that particular message based on the JMSCorrelationID . I am not able to get that particular message. Can you suggest a solution?

public void getSpecificMessage(String queuename, Integer userid) {

         try {
            QueueConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://localhost:61616");
                ((ActiveMQConnectionFactory) connectionFactory).setUseAsyncSend(true);
                QueueConnection connection = connectionFactory.createQueueConnection();
                connection.start();
                QueueSession session = connection.createQueueSession( false,
                     QueueSession.AUTO_ACKNOWLEDGE );
                String id = String.valueOf(userid);
            Queue queue = session.createQueue(queuename);
            QueueReceiver receiver = session.createReceiver(queue, "JMSCorrelationID="+id);
             Message message = receiver.receive();              
        } catch (JMSException e) {              
            e.printStackTrace();
        }
      }

Your first problem is that you are trying to think about the message broker as a database, you must always remember this sage piece of advice, "A message broker is not a database".

There are certain limits on how deep a consumer or Queue browser can go into a destination before the broker will not page in more messages from disk, so you need to check your depth and see if its large than you maxPageSize setting and adjust as needed, but remember that messages paged in remain in memory until consumed.

Just wrap the id value in single quotes

"JMSCorrelationID='"+id+"'"

This functionality is not recommended to be used , there are lot more complications as explained by Tim , but if you want to obsolutely work with it make the change

You can search messages using the MeessageID of a message. This would be fast as messaging providers index messages on message id. There are other way to search based on CorrelationId , meta data etc.

But please remember the primary objective of using a messaging provider is to connect applications in a time independent manner. The receiving application must get messages as soon as possible. If messages are piling up in a queue, it indicates a problem that must be addressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM