Setting up JMS application v42.7.3.1

After creating the queue table and queue for the message types and starting the queue, you can follow these steps to set up your JMS Application:

  1. Create a Connection factory.
  2. Create a Connection using the connection factory.
  3. Create a Session using the connection.
  4. Get the Queue from the session.
  5. Create a message producer using the session and queue to send messages.
  6. Create a message consumer using the session and queue to receive messages.

Connection factory

The Connection factory is used to create connections. EDBJmsConnectionFactory is an implementation of ConnectionFactory and QueueConnectionFactory, used to create Connection and QueueConnection. A Connection factory can be created using one of the constructors of the EDBJmsConnectionFactory class. All three constructors can be used to create either a ConnectionFactory or QueueConnectionFactory.

//Constructor with connection related properties.
public EDBJmsConnectionFactory(String host, int port, String database, 
     String username, String password);
//Constructor with connection string, user name and password.
public EDBJmsConnectionFactory(String connectionString, 
     String username, String password);
//Constructor with SQL Connection.
public EDBJmsConnectionFactory(java.sql.Connection connection);

This example shows how to create a ConnectionFactory using an existing java.sql.Connection:

javax.jms.ConnectionFactory connFactory = new EDBJmsConnectionFactory(connection);

This example shows how to create a QueueConnectionFactory using a connection string, username, and password:

javax.jms.QueueConnectionFactory connFactory = new EDBJmsConnectionFactory
   ("jdbc:edb//localhost:5444/edb", "enterprisedb", "edb");

Connection

A Connection is a client's active connection that can be created from the ConnectionFactory and used to create sessions. EDBJmsConnection is an implementation of Connection, while EDBJmsQueueConnection is an implementation of QueueConnection and extends EDBJmsConnection. A Connection can be created using ConnectionFactory, while QueueConnection can be created from QueueConnectionFactory.

This example shows how to create a Connection and a QueueConnection:

//Connection from ConnectionFactory. Assuming connFactory is ConnectionFactory.
javax.jms.Connection connection = connFactory.createConnection();

////Connection from QueueConnectionFactory. Assuming connFactory is QueueConnectionFactory.
javax.jms.QueueConnection queueConnection = connFactory.createQueueConnection();

A connection must be started in order for the consumer to receive messages. On the other hand, a producer can send messages without starting the connection.

This example shows how to start a connection:

queueConnection.start();

A connection can be stopped at any time to cease receiving messages, and can be restarted when needed. However, a closed connection cannot be restarted.

This example shows how to stop and close the connection:

queueConnection.stop();
queueConnection.close();

Session

The Session in EDBJms is used for creating producers and consumers, and for sending and receiving messages. EDBJmsSession implements the basic Session functionality, while EDBJmsQueueSession extends EDBJmsSession and implements QueueSession. A Session can be created from a Connection.

This example shows how to create a Session and a QueueSession:

// Session
javax.jms.Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
// QueueSession
javax.jms.QueueSession session = queueConnection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

A Session or QueueSession is also used to create queues.

Important

In this context, "creating a queue" does not refer to physically creating the queue. As discussed earlier, the queue needs to be created and started as part of the server-side setup. In this context, creating a queue means getting the queue, related queue table, and payload type that have already been created.

This example shows how to create a queue:

javax.jms.Queue queue = session.createQueue("MSG_QUEUE");

Message producer

A Message producer is responsible for creating and sending messages. It is created using a session and queue. EDBJmsMessageProducer is an implementation of MessageProducer, but in most cases, you will be using the standard MessageProducer.

This example shows how to create a message producer, create a message, and send it. Creating messages of different types will be discussed in the following sections.

javax.jms.MessageProducer messageProducer = session.createProducer(queue);

javax.jms.Message msg = session.createMessage();
msg.setStringProperty("myprop1", "test value 1");

messageProducer.send(msg);

Message consumer

A Message consumer is used to receive messages. It is created using a session and a queue. EDBJmsMessageConsumer is an implementation of MessageConsumer, but you will most often use the standard MessageConsumer.

This example shows how to create a message consumer and receive a message:

javax.jms.MessageConsumer messageConsumer = session.createConsumer(queue);
      
javax.jms.Message message = messageConsumer.receive();