Menu Search

Chapter 4. Examples

The following programs shows how to send and receive messages using the Client. The first program illustrates a point to point example, the second, a pubish/subscribe example.

Both examples show the use JNDI to obtain connection factory and destination objects which the application needs. In this way the configuration is kept separate from the application code itself.

The example code will be straightforward for anyone familiar with JMS. Readers in need of an introduction are directed towards Oracle's JMS tutorial.

4.1. Point to point example

In this example, we illustrate point to point messaging. We create a JNDI context using a properties file, use the context to lookup a connection factory, create and start a connection, create a session, and lookup a destination (a queue) from the JNDI context. Then we create a producer and a consumer, send a message with the producer and receive it with the consumer.

Example 4.1. JMS Example - Point to Point Messaging

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

public class Hello {

    public Hello() {
    }

    public static void main(String[] args) throws Exception {
        Hello hello = new Hello();
        hello.runTest();
    }

    private void runTest() throws Exception {
      Properties properties = new Properties();
      properties.load(this.getClass().getResourceAsStream("helloworld.properties"));  1
      Context context = new InitialContext(properties);                               2

      ConnectionFactory connectionFactory
          = (ConnectionFactory) context.lookup("qpidConnectionFactory");              3
      Connection connection = connectionFactory.createConnection();                   4
      connection.start();                                                             5

      Session session = connection.createSession(true, Session.SESSION_TRANSACTED);   6
      Queue queue = (Queue) context.lookup("myqueue");                                7

      MessageConsumer messageConsumer = session.createConsumer(queue);                8
      MessageProducer messageProducer = session.createProducer(queue);                9

      TextMessage message = session.createTextMessage("Hello world!");                10
      messageProducer.send(message);
      session.commit();

      message = (TextMessage)messageConsumer.receive();                               11
      session.commit();
      System.out.println(message.getText());

      connection.close();                                                             12
      context.close();                                                                13
    }
}
	

1

Loads the JNDI properties file, which specifies the connection factory, queues and topics. See Chapter 6, JNDI Properties Format for details.

2

Creates the JNDI initial context.

3

Looks up a JMS connection factory for Qpid.

4

Creates a JMS connection. Creating the JMS connections establishes the connection to the Broker.

5

Starts the connection, required for the consumption of messages.

6

Creates a transactional session.

7

Looks up a destination for the queue with JNDI name myqueue.

8

Creates a consumer that reads messages from the queue[2].

9

Creates a producer that sends messages to the queue.

10

Creates a new message of type javax.jms.TextMessage, publishes the message and commits the session.

11

Reads the next available message (awaiting indefinitely if necessary) and commits the session.

12

Closes the Connection. All sessions owned by the Connection along with their MessageConsumers and MessageProducers are automatically closed. The connection to the Broker is closed as this point.

13

Closes the JNDI context.

The contents of the helloworld.properties file are shown below.

Example 4.2. JMS Example - Point to Point Messaging - JNDI Properties

java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionFactory = amqp://guest:guest@clientid/?brokerlist='tcp://localhost:5672' 1
queue.myqueue = queue1                                                                                   2
	

1

Defines a connection factory from which Connections can be created. The syntax of a ConnectionURL is given in Chapter 7, Connection URLs.

2

Defines a queue for which MessageProducers and/or MessageConsumers send and receive messages. The format of these entries is described in Section 6.2, “Queue”.



[2] Creating consumer will automatically create the queue on the Broker and bind it to an exchange. Specifically, in this case as the queue. form is used in the JNDI properties the effect will be to create a queue called queue1 on the Broker, and create a binding between the amq.direct exchange and this queue using the queue's name. This process is described in detail in Section 5.6.1, “Consumers have Exchange/Queue Declaration and Binding Side Effect”