Chapter 23
878
The client uses the createTopicConnection() method of the connection factory to create a topic
connection. Connections are resource heavy (each connection requires memory on the server, a
network socket, etc.) so under normal circumstances a client should only create one. Notice that the
connection is specific to the Pub/Sub programming model it is of type
javax.jms.TopicConnection. When a connection is used in the PTP programming model, it is of
type javax.jms.QueueConnection:
// Create a topic connection
topicConnection = connectionFactory.createTopicConnection();
After the constructor creates a connection, it establishes a session, which will be the primary object
through which the client interacts with the broker.
A client must establish a session before it can produce and consume messages. The client uses the
connection to build the session and uses the session to build message producers, consumers, and
messages. Like connections, sessions are also specific to the programming model (in other words there is
a javax.jms.TopicSession and a javax.jms.QueueSession).
The createTopicSession(boolean transacted, int acknowledgeMode) method creates a
topic session. The transacted parameter indicates whether or not the session is transacted. There are
more details about transactions and message acknowledgement in transacted mode in the "Transactions"
section later in this chapter. The acknowledgeMode parameter is valid if the session is non-transacted
and it specifies how message delivery will be acknowledged during the session.
If the session is non-transacted there are three acknowledgement modes:
q
The AUTO_ACKNOWLEDGE mode instructs the session to automatically acknowledge a client's
receipt of a message.
q
The DUPS_OK_ACKNOWLEDGE is similar to the AUTO_ACKNOWLEDGE mode, except that it
instructs the session to lazily acknowledge the delivery of messages. When the session lazily
acknowledges messages it may send a message more than once to the same destination,
whereas if the message is sent in AUTO_ACKNOWLEDGE mode it is delivered once and only
once. This method might result in the duplicate delivery of some messages but it reduces
session overhead because the session does less work to avoid delivering duplicate messages.
q
The CLIENT_ACKNOWLEDGE mode allows the client to acknowledge its receipt of a message by
calling the message's acknowledge() method. When a client acknowledges receipt of a
message it automatically acknowledges the receipt of all messages the session has delivered to
the client up to and including the message the client acknowledges. For example if the session
sends the client three messages and the client calls acknowledge() on the second message
both the first and second messages are acknowledged.
// Create a session
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
The topic is the destination to which the Publisher will send messages. The Publisher must look up the
topic through JNDI:
// Look up a topic
topic = (Topic) ctx.lookup(WeblogicJMSDetails.TOPIC_NAME);