ActiveMQ: how to subscribe/unsubscribe from non-durable subscriptions

1.8k views Asked by At

I have to do a project for a university course I'm taking and am stuck because I cannot find clarification on a simple question:

How do I properly subscribe and unsubscribe from a topic as a non-durable subscriber? It sounds simple enough, but while for durable there is an unsubscribe method, I just don't know what to use for non-durable.

What I want to do is to be able to sub,unsub and resubscribe to any topic at will. I found a post saying you should use consumer.close(); to unsubscribe, but then I don't know how to resubscribe. consumer.start(); isn't recognized as a command (consumer is an instance of the class MessageConsumer).

so unsubscribe = consumer.close(); //right?

resubscribe after unsubscribing = consumer.??? Or do I need to create a new consumer every time I resubscribe?

programming language: Java

2

There are 2 answers

0
Lovegiver On BEST ANSWER

For non-durable subscription, you just need to do a :

consumer.close()

That's what is said here :

1
Tim Bish On

For a non-durable topic subscription there is no such thing as a re-subscribe. You are either activity subscribed or you aren't. The subscribe you create a consumer which will them receive messages sent to the Topic while the consumer is active, to remove that subscription you close the consumer. If you then decide you want to subscribe and listen in on Topic messages you create a new consumer which is an entirely new subscription that will receive again only messages sent while that subscriber is active.