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: Send message flow

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

This example notifies a client of a completed order; it creates a new message, constructs the message text, and sends the message to the client’s queue:

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

   javax.jms.QueueSender sender = qSession.createSender(queue);
   javax.jms.TextMessage textMsg = qSession.createTextMessage(text);

   textMsg.setStringProperty(“ProductDescription”, product);
   textMsg.setIntProperty(“OrderNumber”, orderNo);

   sender.send(textMsg);
}