Sending messages

To send a message, you must specify the destination message queue. The message service notifies listeners that are registered for the queue and the message remains in the queue until it is received and acknowledged.

Figure 31-1 illustrates the message flow that occurs when a client or component sends a message.

In this example, we notify a client of a completed order by creating a new message, constructing the message text, and sending the message to the client’s queue:

public void notifyOrder(MessageService cms, 
                        String queue, 
                        int orderNo, 
                        String product)
{
  String time = new java.util.Date().toString();
  String text = "Order " + orderNo + " for product " 
                + product + " was completed at " + time;

  Message msg = new Message();
  msg.key = cms.getMessageKey();
  msg.props = new Property[2];
  msg.props[0] = new Property("orderNo", 
                              new PropertyValue());
  msg.props[0].value.longValue(orderNo);
  msg.props[1] = new Property("product", 
                              new PropertyValue());
  msg.props[1].value.stringValue(product);
  msg.replyTo = "";
  msg.text = text;
  cms.send(queue, msg, PERSISTENT.value);
}