按消息 ID 从主题中选择消息时遇到问题。 这是简化的代码:
//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
此外,即使我提供了始终为真的选择器 例如"1=1"或空一个 : "", null
尽管它是持久订阅者,但它也不会获取消息。
另一方面,如果我在创建始终为真选择器的消费者之后发布一些内容,它确实会获取此消息。
但像这样的代码确实获取了我所有的消息,包括我一直在寻找的带有 id 的消息
consumer = session.createDurableSubscriber(topic, "SUBS1");
while (msg != null) {
msg = consumer.receive();
}
在我看来,带选择器的 DurableSubscriber 会忽略现有消息。虽然我在 jms 1.1 规范中没有找到类似的内容
到目前为止,我只尝试将 ActiveMQ 5.5.1 作为 JMS 提供程序
问题是我做错了什么还是错误?
最佳答案
如果您“在”向主题发送消息后连接到主题(并且您这样做了),那么您将无法接收该消息。除非使用持久订阅者并且它是在消息发送到主题“之前”创建的。
主题中仅存储 Activity 的非持久订阅者和已创建的持久订阅者的消息。即使他们处于离线状态。
您可以使用使用特定 correlationId 的选择器创建持久订阅者,然后将该 correlationId 设置为您的消息并将其发送到主题。
关于java - JMS:无法通过选择器从主题中选择 jms 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8741848/