Sending a message

 

An application sends messages using a MessageProducer object. A MessageProducer object is normally created for a specific destination so that all messages sent using that message producer are sent to the same destination. The destination is specified using either a Queue or a Topic object. Queue and Topic objects can be created at runtime, or built and stored in a JNDI namespace, as described in Destinations.

After a Queue or a Topic object is obtained, an application can pass the object to the createProducer() method to create a MessageProducer object, as shown in the following example:

MessageProducer messageProducer = session.createProducer(ioDestination);
The parameter ioDestination can be either a Queue or a Topic object.

The application can then use the send() method on the MessageProducer object to send messages. Here is an example:

messageProducer.send(outMessage);
We can use the send() method to send messages in either messaging domain. The nature of the destination determines the actual domain used. However, TopicPublisher, the sub-interface for MessageProducer that is specific to the publish/subscribe domain, uses a publish() method instead.

An application can create a MessageProducer object with no specified destination. In this case, the application must specify the destination in 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.


uj25080_