Creating connections

JMS provides two types of connections, one for message queues and one for topics. To create a connection from a JMS client application to EAServer, you must have access to a ConnectionFactory object. See “Looking up a ConnectionFactory object”. Once you have created a connection, you must explicitly start it before EAServer can deliver messages on the connection. To avoid message delivery before a client has finished setting up, you may want to delay starting the connection. This code fragment creates a QueueConnection object and starts the connection:

QueueConnection queueConn =
   queueCF.createQueueConnection();

// other setup procedures

queueConn.start();

This sample creates a connection object for a topic and starts the connection:

TopicConnection topicConn = 
   topicCF.createTopicConnection();

// other setup procedures

topicConn.start();

You can also stop delivery of messages using the QueueConnection.stop method, then use start to resume delivery. While a connection is stopped, receive calls do not return with a message, and messages are not delivered to message listeners. Any calls to receive or message listeners that are in progress when stop is called, complete before the stop method returns.

With a single connection to EAServer, the message service can send and receive multiple messages. Therefore, a JMS client usually only needs one connection.

Setting the client ID

A connection for a durable topic subscriber must have a client ID associated with it so that EAServer can uniquely identify a client if it disconnects and later reconnects. You can use EAServer Manager to set the client ID when you create the topic connection factory; see Chapter 8, “Setting up the Message Service,” in the EAServer System Administration Guide. If you do not set the client ID in the connection factory, you must set it immediately after creating the connection and before performing any other action on the connection. After this point, attempting to set the client ID throws an IllegalStateException. This code fragment illustrates how to set a topic connection’s client ID:

topicConn.setClientID(“Client ID String”);

For more information about topic subscribers, see “Creating message consumers”.

ExceptionListener

To enable EAServer to asynchronously notify clients of serious connection problems, create and register an ExceptionListener. The javax.jms.ExceptionListener must implement this method:

void onException(JMSException exception);

To register a listener, call the Connection::setExceptionListener method, for example:

queueConn.setExceptionListener(MyExceptionListener);

If an exception occurs and a listener has been registered, EAServer calls the onException method and passes the JMSException, which describes the problem.