When you publish a message, a copy is sent to all topic subscribers that have a message selector registered with the specified topic. Figure 31-2 illustrates the message flow when a client or component publishes a message.
Figure 31-2: Publish message flow
This example publishes a message that notifies clients of changes in a stock value; it creates the message text, creates a TopicPublisher and the message using the TopicSession object, and publishes the message:
public void notifyStockValue(TopicSession tSession, Topic topic, String stock, double value) { String time = new java.util.Date().toString(); String text = time + ": The stock " + stock + " has value " + value; // Create the publisher and message objects. javax.jms.TopicPublisher publisher = tSession.createPublisher(topic); javax.jms.TextMessage textMsg = tSession.createTextMessage(text); // Publish a non-persistent message with a priority of 9 and a // lifetime of 5000 milliseconds (5 seconds) publisher.publish(textMsg, DeliveryMode.NON_PERSISTENT, 9, 5000); }
To publish a persistent message using the default priority (4) and timeout (never expires) values, use this syntax:
publisher.publish(textMsg);
Copyright © 2005. Sybase Inc. All rights reserved. |