Sending messages in a JMS application

Before a JMS application can send messages to a destination, it must first create a MessageProducer object for the destination. To send a message to the destination, the application creates a Message object and then calls the send() method of the MessageProducer object.

An application uses a MessageProducer object to send messages. An application normally creates a MessageProducer object for a specific destination, which can be a queue or a topic, so that all messages sent using the message producer are sent to the same destination. Therefore, before an application can create a MessageProducer object, it must first create a Queue or Topic object. For information about how to create a Queue or Topic object, see the following topics:

To create a MessageProducer object, an application uses the createProducer() method of a Session object, as shown in the following example:
MessageProducer producer = session.createProducer(destination);
The parameter destination is a Queue or Topic object that the application has created previously. Before an application can send a message, it must create a Message object. The body of a message contains the application data, and JMS defines five types of message body:

  • Bytes
  • Map
  • Object
  • Stream
  • Text
Each type of message body has its own JMS interface, which is a sub-interface of the Message interface, and a method in the Session interface for creating a message with that type of body. For example, the interface for a text message is called TextMessage, and an application uses the createTextMessage() method of a Session object to create a text message, as shown in the following statement:
TextMessage outMessage = session.createTextMessage(outString);
For more information about messages and message bodies, see JMS messages. To send a message, an application uses the send() method of a MessageProducer object, as shown in the following example:
producer.send(outMessage);

An application can use the send() method to send messages in either messaging domain. The nature of the destination determines which messaging domain is used. However, TopicPublisher, the sub-interface of MessageProducer that is specific to the publish/subscribe domain, also has a publish() method, which can be used instead of the send() method. The two methods are functionally the same.

An application can create a MessageProducer object with no specified destination. In this case, the application must specify the destination when calling the send() method.

If an application sends a message within a transaction, the message is not delivered to its destination until the transaction is committed. This means that an application cannot send a message and receive a reply to the message within the same transaction.

A destination can be configured so that when an application sends messages to it, IBM MQ classes for JMS forwards the message and returns control back to the application without determining whether the queue manager has received the message safely. This is sometimes referred to as asynchronous put. For more information, see Putting messages asynchronously in IBM MQ classes for JMS.