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.
MessageProducer producer = session.createProducer(destination);
The
parameter destination is a Queue or Topic object that the application has created previously.
- Bytes
- Map
- Object
- Stream
- Text
TextMessage outMessage = session.createTextMessage(outString);
For
more information about messages and message bodies, see JMS messages.
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.