Overview

This document provides code examples demonstrating how to send and receive messages using the publish-and-subscribe and point-to-point messaging styles.

Preparatory work

OpenJMS uses JNDI (the Java Naming and Directory Interface ), to store connection factories, topics, and queues.

In order to use either of the JMS messaging styles, you must first create a JNDI Context :

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;

// ...

    Hashtable properties = new Hashtable();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, 
                   "org.exolab.jms.jndi.InitialContextFactory");
    properties.put(Context.PROVIDER_URL, "rmi://localhost:1099/");

    Context context = new InitialContext(properties);
        

Publish-and-Subscribe

This section shows how to send and receive messages using Publish-and-Subscribe messaging.

Retrieving a TopicConnectionFactory

The message publisher and message subscriber need to look up a TopicConnectionFactory from JNDI, in order to create a TopicConnection and TopicSession .

OpenJMS is pre-configured with a TopicConnectionFactory named "JmsTopicConnectionFactory" , whic h can be retrieved as follows:

import javax.jms.TopicConnectionFactory;

// ...

    TopicConnectionFactory factory = 
        (TopicConnectionFactory) context.lookup("JmsTopicConnectionFactory");
          

Creating a TopicConnection

The TopicConnectionFactory can then be used to create a TopicConnection :

import javax.jms.TopicConnection;

// ...

    TopicConnection connection = factory.createTopicConnection();
    connection.start();
          

Creating a TopicSession

import javax.jms.Session;
import javax.jms.TopicSession;

// ...

    TopicSession session = 
        connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
          

Retrieving a Topic

The message publisher and message subscriber need to look up a Topic from JNDI in order to publish and subscribe to messages.

OpenJMS is pre-configured with a Topic named "topic1" , which can be retrieved as follows:

import javax.jms.Topic;

// ...

    Topic topic = (Topic) context.lookup("topic1");
          

Sending messages to a Topic

import javax.jms.TextMessage;
import javax.jms.TopicPublisher;

// ...

    TopicPublisher publisher = session.createPublisher(topic);
    TextMessage message = session.createTextMessage("Hello World!");
    publisher.publish(message);       
          

Receiving messages from a Topic

The following example shows how to synchronously receive messages from a Topic :

import javax.jms.TextMessage;
import javax.jms.TopicSubscriber;

// ...

    TopicSubscriber subscriber = session.createSubscriber(topic);
    TextMessage message = (TextMessage) subscriber.receive();
    System.out.println("Received message: " + message.getText());
          

In the above, the subscriber blocks until it receives a message.

Point-to-Point

This section demonstrates how to send and receive messages using Point-to-Point messaging.

Retrieving a QueueConnectionFactory

The message sender and message receiver need to look up a QueueConnectionFactory from JNDI, in order to create a QueueConnection and QueueSession .

OpenJMS is pre-configured with a QueueConnectionFactory named "JmsQueueConnectionFactory" , which can be retrieved as follows:

import javax.jms.QueueConnectionFactory;

// ...

    QueueConnectionFactory factory = 
        (QueueConnectionFactory) context.lookup("JmsQueueConnectionFactory");
          

Creating a QueueConnection

The QueueConnectionFactory can then be used to create a QueueConnection :

import javax.jms.QueueConnection;

// ...

    QueueConnection connection = factory.createQueueConnection();
    connection.start();
          

Creating a QueueSession

import javax.jms.Session;
import javax.jms.QueueSession;

// ...

    QueueSession session = 
        connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
          

Retrieving a Queue

The message sender and message receiver need to look up a Queue from JNDI in order to send and receive messages.

OpenJMS is pre-configured with a Queue named "queue1" , which can be retrieved as follows:

import javax.jms.Queue;

// ...

    Queue queue = (Queue) context.lookup("queue1");
          

Sending messages to a Queue

The following example shows how to send messages to a Queue :

import javax.jms.QueueSender;
import javax.jms.TextMessage;

// ...

    QueueSender sender = session.createSender(queue);
    TextMessage message = session.createTextMessage("Hello World!");
    sender.send(message);       
          

Receiving messages from a Queue

The following example shows how to asynchronously receive messages from a Queue :

import javax.jms.JMSException;
import javax.jms.MessageListener;
import javax.jms.QueueReceiver;
import javax.jms.TextMessage;

// ...

    QueueReceiver receiver = session.createReceiver(queue);
    receiver.setMessageListener(new MessageListener() {
        public void onMessage(Message message) {
            TextMessage text = (TextMessage) message;
            System.out.println("Received message: " + message.getText());
        }
    });