Publishing messages

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.

This example illustrates how to publish a message that notifies clients of changes in a stock value. Set the message topic, define the message text, set the message key, define and set message properties, and call publish:

public void notifyStockValue(MessageService cms,
                             String stock,
                             double value)
{
   String topic = "StockValue." + stock;
   String time = new java.util.Date().toString();
   String text = time + ": The stock " + stock + 
                        " has value " + value;

   Message msg = new Message();
   msg.key = cms.getMessageKey();
   msg.props = new Property[2];
   msg.props[0] = new Property("stock", 
                               new PropertyValue());
   msg.props[0].value.stringValue(stock);
   msg.props[1] = new Property("value", 
                               new PropertyValue());
   msg.props[1].value.doubleValue(value);
   msg.replyTo = "";
   msg.text = text;
   cms.publish(topic, msg, 0);
}

To publish a persistent message using the default priority (4) and timeout (never expires) values, use this syntax:

publisher.publish(textMsg);