How to consume messages from JMS Topic in Java

319 Views Asked by At

Yes, I have googled but it's very confusing to me. I want to continuously receive messages from a Topic (it's an oracle AQ topic) similar to the way that I use the MessageListener interface onMessage method for Queues. I can't seem to find a clear answer

With Queues - it's simple. Create an MDB publisher/producer class to send messages, and a Consumer class that implements MessageListener to receive messages. With Topics - there seems to be a different strategy when it comes to receiving.

I have tried examples that implement the MessageConsumer interface as well as MessageListener interface but nothing is really clear cut and nothing works for me. I have tried the subscriber.receive() method using MessageConsumer to create a durable subscriber on the topic session - not only does it not work but it only happens when you call the receive method, but I want something that will consume messages as and when they come.

1

There are 1 best solutions below

0
Justin Bertram On

Both MessageListener and MessageConsumer should work. With JMS topics it's important to understand that you must create the subscription before messages are sent. Any message sent to the topic before the subscription is created will not be dispatched to that subscription's consumer. Also, ensure you invoke start() on the Connection.