Original address: http://blog.chenlb.com/2010/01/jms-topic-durable-subscriber.html
The Topic mechanism of message middleware does not normally save messages. Without a connection, you will not receive a message when you connect again without losing your connection. This mechanism is well applied to scenarios where messages can be lost. Of course, message middleware has the ability to save messages. Durablesubscriber is defined in the JMS specification.
A small segment of a persistent subscription in the JMS specification
Non-persistent subscriptions persist to the life cycle of their subscribed objects. This means that clients can only
see messages published by Related topics when the Subscriber is active. If the Subscriber is inactive, it misses the message for the related topic. The
subscriber can be defined as durable (persisted) if it costs a large amount of overhead. Persistent Subscriber
Registers a persistent subscription (subscription) with a unique identity maintained by JMS. Subsequent subscribers with the same identity
will renew the subscription status of the previous subscriber. If a persistent subscription does not have an active subscriber, JMS keeps the subscription
message until the message is received or expired by the subscription.
Looking at the JMS specification (which may be poorly translated), look at the key code for a good understanding.
To use persistent subscriptions, the sender of the message is found in the Deliverymode.persistent mode and set before the connection. The Subscriber will set the client name and invoke Session.createdurablesubscriber.
Sender:session session = Connection.createsession (false, Session.auto_acknowledge); Destination Destination = Session.createtopic ("My-topic"); MessageProducer producer = Session.createproducer (destination); Producer.setdeliverymode (deliverymode.persistent); Set Save message Connection.start (); When you are finished setting up, you will connect
Session session = Connection.createsession (false, Session.auto_acknowledge);
Destination Destination = Session.createtopic ("My-topic");
MessageProducer producer = Session.createproducer (destination);
Producer.setdeliverymode (deliverymode.persistent); Set Save message
Connection.start ()///set up before connecting
Receiver:connection.setClientID ("Client-name"); Final session session = Connection.createsession (false, Session.auto_acknowledge); Topic Topic = session.createtopic ("My-topic"); Messageconsumer consumer = session.createdurablesubscriber (topic, "my-sub-name"); Connection.start ();
Connection.setclientid ("Client-name");
Final session session = Connection.createsession (false, Session.auto_acknowledge);
Topic Topic = session.createtopic ("My-topic");
Messageconsumer consumer = session.createdurablesubscriber (topic, "My-sub-name");
Connection.start ();
Finally, run the Receiver, the purpose is to register this client (so that the message middleware server for this customer to save the message), and then shut down the Receiver, start Sender, discover the message, and then start Receiver can receive offline messages.
Common subscribers can be enabled at the same time: messageconsumer consumer = session.createconsumer (topic); For comparison.
In use, the message server keeps separate messages for each offline registered client, and they are sent out when they are online.
This mechanism is like a lecture: The teacher is lecturing, students with a tape recorder can sign in after skipping classes, and a durable subscriber is a student with a tape recorder.
Question: Topic persistence is a must to declare that a persistent message is required before the Subscriber. Or, after the message is saved at the Publisher, registered persistent subscriptions can receive offline messages.