简体   繁体   English

JMS:无法通过选择器从主题中选择JMS消息

[英]JMS: cannot select jms message from topic by selector

Got a problem with selecting message from topic by message id. 从主题按消息ID选择消息时出现问题。 Here's the boiled down code: 这是简化的代码:

//publish message
connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); 
//or external broker: tcp://localhost:61616

con = connectionFactory.createConnection();
con.setClientID("foo");
con.start();
session = connection.createSession(true, Session.SESSION_TRANSACTED);
topic = session.createTopic("topic_name");
producer = session.createProducer(topic);
//create text message
producer.send(message);
messageId = message.getJMSMessageID();
session.commit();
//close all stuff

//get message by id (the same VM split second after publishing)
//get connection the same way as for publishing
session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
topic = session.createTopic("topic_name");
consumer = session.createDurableSubscriber(topic, "SUBS1", "JMSMessageID='messageId'", false);
//here we get stuck though the message IS there
msg = consumer.receive(); //receiveNoWait gives null

moreover even if I provide selector which is always true eg "1=1" or empty one : "", null 而且即使我提供的选择器始终为真,例如“1 = 1”或空的:“”,null

it does not fetch messages too despite it is durable subscriber. 尽管它是持久的订户,但它也不会提取消息。

On the other hand if I post something after consumer with alsways true selector was created it does fetch this message. 另一方面,如果我在消费者之后使用alsways发布了一些真正的选择器,那么它确实会获取此消息。

but code like this DOES fetch all my messages including the one with id i've been looking for 但是这样的代码确实获取了我所有的消息,包括我一直在寻找的具有id的消息

consumer = session.createDurableSubscriber(topic, "SUBS1");
while (msg != null) {
    msg = consumer.receive();
}

It looks to me that DurableSubscriber with selector ignores existing messages. 在我看来,带有选择器的DurableSubscriber会忽略现有消息。 Though I didn't find anything like that in the jms 1.1 spec 虽然我在jms 1.1规范中没有找到类似的东西

So far I tried only ActiveMQ 5.5.1 as JMS provider 到目前为止,我只尝试使用ActiveMQ 5.5.1作为JMS提供程序

The question is am I doing something wrong or it is a bug? 问题是我做错了什么或者是一个错误?

If you connect to a topic "after" a message was sent to it (and you do) then there is no way you can receive the message. 如果您在“之后”连接到某个主题,则向其发送了一条消息(您可以这样做),那么您将无法接收到该消息。 Unless durable subscriber is used and it had been created "before" the message was sent to the topic. 除非使用持久订阅者,并且“之前”已将消息发送到主题。

A message is stored in the topic only for active non durable subscribers and for durable subscribers that are already created. 仅对于活动的非持久性订户和已创建的持久性订户,一条消息存储在主题中。 Even if they are offline. 即使他们离线了。

You can create durable subscriber with selector that uses particular correlationId and then set that correlationId to your message and send it to the topic. 您可以使用选择器创建持久订阅者,该选择器使用特定的correlationId,然后将该correlationId设置为您的消息并将其发送到主题。

When a messaging provider gets publication from a producer, it checks to see if there are any subscriptions that match the publication topic. 当消息传递提供程序从生产者获取发布时,它会检查是否存在与发布主题匹配的任何预订。 If matching subscriptions are found, then publication is delivered to those subscribers. 如果找到匹配的订阅,则将发布发布给这些订阅者。 So subscriptions must be created first before publishing messages. 因此,必须在发布消息之前首先创建订阅。

From what I recall selectors behave like SQL, so you need to ensure you're actually selecting against valid properties on the message. 我记得选择器的行为类似于SQL,因此您需要确保实际上是根据消息上的有效属性进行选择。

Try changing your selector to `"JMSMessageID='ID:<messageId>'" and see whether that works. 尝试将选择器更改为`“” JMSMessageID ='ID:<messageId>'“,然后查看是否可行。

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

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