background image

The createDurableSubscriber Method

<< Advanced Reliability Mechanisms | Nondurable Subscribers and Subscriptions >>
<< Advanced Reliability Mechanisms | Nondurable Subscribers and Subscriptions >>

The createDurableSubscriber Method

At the cost of higher overhead, you can use the Session.createDurableSubscriber method to
create a durable subscriber. A durable subscription can have only one active subscriber at a
time.
A durable subscriber registers a durable subscription by specifying a unique identity that is
retained by the JMS provider. Subsequent subscriber objects that have the same identity resume
the subscription in the state in which it was left by the preceding subscriber. If a durable
subscription has no active subscriber, the JMS provider retains the subscription's messages until
they are received by the subscription or until they expire.
You establish the unique identity of a durable subscriber by setting the following:
A client ID for the connection
A topic and a subscription name for the subscriber
You set the client ID administratively for a client-specific connection factory using the Admin
Console.
After using this connection factory to create the connection and the session, you call the
createDurableSubscriber
method with two arguments: the topic and a string that specifies
the name of the subscription:
String subName =
"MySub";
MessageConsumer topicSubscriber =
session.createDurableSubscriber(myTopic, subName);
The subscriber becomes active after you start the Connection or TopicConnection. Later, you
might close the subscriber:
topicSubscriber.close();
The JMS provider stores the messages sent or published to the topic, as it would store messages
sent to a queue. If the program or another application calls createDurableSubscriber using
the same connection factory and its client ID, the same topic, and the same subscription name,
the subscription is reactivated, and the JMS provider delivers the messages that were published
while the subscriber was inactive.
To delete a durable subscription, first close the subscriber, and then use the unsubscribe
method, with the subscription name as the argument:
topicSubscriber.close();
session.unsubscribe(
"MySub");
The unsubscribe method deletes the state that the provider maintains for the subscriber.
Figure 31­7
and
Figure 31­8
show the difference between a nondurable and a durable
subscriber. With an ordinary, nondurable subscriber, the subscriber and the subscription begin
and end at the same point and are, in effect, identical. When a subscriber is closed, the
Creating Robust JMS Applications
Chapter 31 · The Java Message Service API
943