Setting up the JMS application v42.7.3.2
After creating the queue table and queue for the message types and starting the queue, you can set up your JMS application:
- Create a connection factory.
- Create a connection using the connection factory.
- Create a session using the connection.
- Get the queue from the session.
- Create a message producer using the session and queue to send messages.
- Create a message consumer using the session and queue to receive messages.
Connection factory
Use the connection factory to create connections. EDBJmsConnectionFactory
is an implementation of ConnectionFactory
and QueueConnectionFactory
, which you use to create Connection
and QueueConnection
. You can create a connection factory using one of the constructors of the EDBJmsConnectionFactory
class. You can use all three constructors to create either a ConnectionFactory
or QueueConnectionFactory
.
This example shows how to create a ConnectionFactory
using an existing java.sql.Connection
:
This example shows how to create a QueueConnectionFactory
using a connection string, username, and password:
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
, and EDBJmsQueueConnection
is an implementation of QueueConnection
and extends EDBJmsConnection
. You can create a Connection
using ConnectionFactory
and a QueueConnection
from QueueConnectionFactory
.
This example shows how to create a Connection
and a QueueConnection
:
You must start a connection for the consumer to receive messages. However, a producer can send messages without starting the connection.
This example shows how to start a connection:
You can stop a connection at any time to stop receiving messages, and you can restart it when needed. However, you can't restart a closed connection.
This example shows how to stop and close the connection:
Session
A session in EDBJms is used for creating producers and consumers and for sending and receiving messages. EDBJmsSession
implements the basic Session
functionality, and 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
:
You can also use a Session
or QueueSession
to create queues.
Important
In this context, "creating a queue" doesn't refer to physically creating the queue. As discussed earlier, you need to create and start the queue as part of the server-side setup. In this context, creating a queue means getting the queue, related queue table, and payload type that were already created.
This example shows how to create a queue:
Message producer
A message producer is responsible for creating and sending messages. You create it using a session and queue. EDBJmsMessageProducer
is an implementation of MessageProducer
, but in most cases you use the standard MessageProducer
.
This example shows how to create a message producer, create a message, and send it. To create messages of different types, see Message types.
Message consumer
A message consumer receives messages. You create it using a session and a queue. EDBJmsMessageConsumer
is an implementation of MessageConsumer
, but you'll most often use the standard MessageConsumer
.
This example shows how to create a message consumer and receive a message: