Chapter 3. Using the Qpid JMS client

Table of Contents

1. A Simple Messaging Program in Java JMS
2. Apache Qpid JNDI Properties for AMQP Messaging
2.1. JNDI Properties for Apache Qpid
2.2. Connection URLs
3. Java JMS Message Properties
4. JMS MapMessage Types
5. JMS Client Logging

1. A Simple Messaging Program in Java JMS

The following program shows how to use address strings and JNDI for Qpid programs that use Java JMS.

The Qpid JMS client uses Qpid Messaging API Section 4, “Addresses” to identify sources and targets. This program uses a JNDI properties file that defines a connection factory for the broker we are using, and the address of the topic exchange node that we bind the sender and receiver to. (The syntax of a ConnectionURL is given in Section 2, “Apache Qpid JNDI Properties for AMQP Messaging”.)

Example 3.1. JNDI Properties File for "Hello world!" example

java.naming.factory.initial 
  = org.apache.qpid.jndi.PropertiesFileInitialContextFactory

# connectionfactory.[jndiname] = [ConnectionURL]
connectionfactory.qpidConnectionfactory 
  = amqp://guest:guest@clientid/test?brokerlist='tcp://localhost:5672'
# destination.[jndiname] = [address_string]
destination.topicExchange = amq.topic

In the Java JMS code, we use create a JNDI context, use the context to find a connection factory and create and start a connection, create a session, and create a destination that corresponds to the topic exchange. Then we create a sender and a receiver, send a message with the sender, and receive it with the receiver. This code should be straightforward for anyone familiar with Java JMS.

Example 3.2. "Hello world!" in Java

package org.apache.qpid.example.jmsexample.hello;

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) {
    Hello producer = new Hello();
    producer.runTest();
  }

  private void runTest() {
    try {
      Properties properties = new Properties();
      properties.load(this.getClass().getResourceAsStream("hello.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(false,Session.AUTO_ACKNOWLEDGE);(6)
      Destination destination = (Destination) context.lookup("topicExchange");  (7)

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

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

      message = (TextMessage)messageConsumer.receive();    (10)
      System.out.println(message.getText());

      connection.close();  (11)
      context.close();   (12)
    }
    catch (Exception exp) {
      exp.printStackTrace();
    }
  }
}
	

(1)

Loads the JNDI properties file, which specifies connection properties, queues, topics, and addressing options. See Section 2, “Apache Qpid JNDI Properties for AMQP Messaging” for details.

(2)

Creates the JNDI initial context.

(3)

Creates a JMS connection factory for Qpid.

(4)

Creates a JMS connection.

(5)

Activates the connection.

(6)

Creates a session. This session is not transactional (transactions='false'), and messages are automatically acknowledged.

(7)

Creates a destination for the topic exchange, so senders and receivers can use it.

(8)

Creates a producer that sends messages to the topic exchange.

(9)

Creates a consumer that reads messages from the topic exchange.

(10)

Reads the next available message.

(11)

Closes the connection, all sessions managed by the connection, and all senders and receivers managed by each session.

(12)

Closes the JNDI context.