NonStop JMS User's Manual (NonStop JMS 2.0+)

NSJMS and JMS Client Applications
NonStop Server for Java Message Service User’s Manual522356-002
3-10
Receiving Messages
This code fragment shows the use of instanceof when pulling the contents from a
TextMessage:
if (inMessage instanceof TextMessage){
String replyString =((TextMessage)inMessage).getText();
.
.
.
} else {
System.out.println("Reply message was not a TextMessage");
}
Receiving Messages from a Topic
To receive a message from a topic, you create a TopicSubscriber using the
createSubscriber() method. This method creates TopicSubscribers as either non-
durable or durable subscribers.
Non-durable Subscribers
These subscribers only receive messages that are published on a chosen topic while
the subscriber is active.
To create a non-durable subscriber use the createSubscriber() method for the
session object. For example:
TopicSubscriber subscriber = session.createSubscriber(topic);
Non-durable subscribers automatically terminate themselves when their close()
method is called.
Durable Subscribers
These subscribers receive all messages published on a topic, including messages
published while the subscriber is inactive.
To create a durable subscriber use the createDurableSubscriber() method for
the session object. This method is similar to creating a non-durable subscriber, except
that you pass a parameter to uniquely identify the subscriber. For example:
TopicSubscriber subscriber =
session.createDurableSubscriber(topic,MY_SUB_01);
To terminate a durable subscriber use the unsubscribe() method and pass in the
unique name that created the subscriber. For example:
session.unsubscribe(MY_SUB_01);