简体   繁体   English

如何在JMS队列中搜索特定消息

[英]How to search for a particular message in JMS queue

I am sending some messages to a JMS queue. 我正在向JMS队列发送一些消息。 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: 我通过以下方式进行了尝试:在将消息发送到队列时设置JMSCorrelationID

 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 . 在使用者代码中,我想基于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. 使用者或Queue浏览器可以进入目的地的深度有一定限制,在代理不会从磁盘中分页更多消息之前,您需要检查深度并查看深度是否大于maxPageSize设置并根据需要进行调整,但请记住,分页的消息会保留在内存中直到被消耗。

Just wrap the id value in single quotes 只需将id值用单引号引起来

"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 不建议使用此功能,如Tim所述,还有很多复杂性,但是如果您要绝对使用它,请进行更改

You can search messages using the MeessageID of a message. 您可以使用消息的MeessageID搜索消息。 This would be fast as messaging providers index messages on message id. 当消息传递提供者在消息ID上索引消息时,这将很快。 There are other way to search based on CorrelationId , meta data etc. 还有其他基于CorrelationId ,元数据等进行搜索的方法。

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. 如果消息堆积在队列中,则表明必须解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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