NonStop JMS User's Manual (H06.03+, J06.03+, NonStop JMS 3.0+)

NSJMS and JMS Client Applications
NonStop Server for Java Message Service User’s Manual522356-006
4-14
Best Practices for Developing a JMS Client
Topic Publisher/Subscriber Sample Setup (Recommended)
This is a sample of the recommended setup process in which the setup steps are
performed only once:
TopicConnectionFactory factory = TopicConnectionFactory)
jndiContext.lookup("TopicConnectionFactory");
TopicConnection connection = factory.createTopicConnection();
TopicSession sessionSub = connection.createTopicSession(true,
Session.AUTO_ACKNOWLEDGE);
TopicSession sessionPub = connection.createTopicSession(true,
Session.AUTO_ACKNOWLEDGE);
Topic topicSub = (Topic)jndiContext.lookup(topicSubName);
Topic topicPub = (Topic)jndiContext.lookup(topicPubName);
TopicSubscriber subscriber =
sessionSub.createDurableSubscriber(topicSub,
"MySubscription");
TopicPublisher publisher =
sessionPub.createPublisher(topicPub);
TextListener topicListener = new TextListener();
subscriber.setMessageListener(topicListener);
connection.start();
class TextListener implements MessageListener
{
public void onMessage(Message message)
{
TextMessage messageSub = (TextMessage) message;
// Only the message processing, i.e. publish() of the
// message is done after each receive
// Most efficient
TextMessage messagePub =sessionPub.createTextMessage();
messagePub.setText(messageSub.getText());
publisher.publish(messagePub);
// session commits can be done after each message or
// batched
}