Show TOC Start of Content Area

Procedure documentation Creating Message Producers and Consumers  Locate the document in its SAP Library structure

Use

To send messages, you have to create specific message producers and consumers. There are different types of consumers and producers:

      If you have a queue, you create a queue sender and receiver.

      If you have a topic, you create a topic publisher and subscriber.

There are two ways to receive a message: synchronously and asynchronously.

A synchronous message consumer explicitly requests messages from the server (that is, it is the active part of the client-server interaction). An asynchronous message consumer is provided with messages by the server when such are available rather than explicitly requesting them (that is, it is the passive part of the client-server interaction). Message-driven beans are examples of asynchronous message consumers.

Procedure

Creating a Message Producer to a Queue

To create a message sender, use the following code:

// create a message producer

QueueSender queueSender = queueSession.createSender(queue);

Having obtained a QueueSender, use it to send messages to the specified queue.

Creating a Message Consumer to a Queue

In the point-to-point messaging scenario, you can choose between the following two ways to receive messages:

      If you want to receive messages asynchronously, implement the MessageListener interface. Then use the onMessage(javax.jms.Message) method to receive:

public class Example_class implements javax.jms.MessageListener {

   

    ...

    // create a queue receiver

    QueueReceiver queueReceiver = 

                          queueSession.createReceiver(queue);

    // set a message listener

    queueReceiver.setMessageListener(this);

    ...

 

  public void onMessage(javax.jms.Message message) {

    // process the message

      If you want to receive messages synchronously, use the receive() method:

// create subscriber to a queue

QueueReceiver queueReceiver = queueSession.createReceiver(queue);

 

// receive message

TextMessage textMessage = (TextMessage) queueReceiver.receive();

Creating a Message Producer to a Topic

Create a topic publisher using the following code:

TopicPublisher topicPublsiher =

    topicSession.createTopicPublisher(topic);

Creating a Message Consumer to a Topic

Here we refer to synchronous and asynchronous to describe how a JMS consumer application receives messages. The overall JMS client-to-client communication process itself is always asynchronous, since the producer application only waits until the message is received by the JMS Provider, not until the message is successfully received and processed by the consumer, to complete its next task.

You can use the TopicSubscriber in two ways:

      If you want to receive messages asynchronously, you must register the class as a MessageListener instance. This enables you to receive the messages in the onMessage(Message) method:

// create subscriber to a topic

TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);

 

// set message listener

topicSubscriber.setMessageListener(this);

 

public void onMessage(javax.jms.Message message) {

// process the message

      If you want to receive messages synchronously, use the receive() method.

// create subscriber to a topic

TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);

 

// receive a text message

TextMessage textMessage = (TextMessage) topicSubscriber.receive();

 

End of Content Area