Building a connection in a JMS application

To build a connection, a JMS application uses a ConnectionFactory object to create a Connection object and then starts the connection.

To create a Connection object, an application uses the createConnection() method of a ConnectionFactory object, as shown in the following example:

ConnectionFactory factory;
Connection connection;
.
.
.
connection = factory.createConnection();

When a JMS connection is created, the IBM® MQ classes for JMS creates a connection handle (Hconn) and starts a conversation with the queue manager.

The QueueConnectionFactory interface and the TopicConnectionFactory interface each inherits the createConnection() method from the ConnectionFactory interface. You can therefore use the createConnection() method to create a domain specific object, as shown in the following example:

QueueConnectionFactory qcf;
Connection connection;
.
.
.
connection = qcf.createConnection();
This fragment of code creates a QueueConnection object. An application can now perform a domain independent operation on this object, or an operation that is applicable only to the point-to-point domain. However, if the application attempts to perform an operation that is applicable only to the publish/subscribe domain, an IllegalStateException exception is thrown with the following message:

JMSMQ1112: Operation for a domain specific object was not valid.
           Operation createProducer() is not valid for type com.ibm.mq.jms.MQTopic
This is because the connection was created from a domain specific connection factory.
Note: Note that the application process ID is used as the default user identity to be passed to the queue manager. If the application is running in client transport mode then this process ID must exist, with the relevant authorizations, on the server. If you want a different identity to be used, then use the createConnection(username, password) method.
The JMS specification states that a connection is created in the stopped state. Until a connection starts, a message consumer that is associated with the connection cannot receive any messages. To start a connection, an application uses the start() method of a Connection object, as shown in the following example:

connection.start();