Reputation: 317
Got a problem with selecting message from topic by message 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 e.g. "1=1" or empty one : "", 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.
but code like this DOES fetch all my messages including the one with id i've been looking for
consumer = session.createDurableSubscriber(topic, "SUBS1");
while (msg != null) {
msg = consumer.receive();
}
It looks to me that DurableSubscriber with selector ignores existing messages. Though I didn't find anything like that in the jms 1.1 spec
So far I tried only ActiveMQ 5.5.1 as JMS provider
The question is am I doing something wrong or it is a bug?
Upvotes: 1
Views: 3039
Reputation: 15273
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.
Upvotes: 1
Reputation: 175
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.
Upvotes: 2
Reputation: 11638
From what I recall selectors behave like SQL, so you need to ensure you're actually selecting against valid properties on the message.
Try changing your selector to `"JMSMessageID='ID:<messageId>'" and see whether that works.
Upvotes: 0