New in JMS 1.1: Common interfaces
In this section, we'll look at each of the important JMS common interfaces for JMS client programming and some of their methods. It is important to remember that although the common interfaces are domain-independent, the behavior of client code matches the domain-specific behavior for the domain in operation at runtime.
In the next section (Client programming with common interfaces ), we'll look at some sample code that performs JMS message processing.
ConnectionFactory is an administered object that is retrieved from JNDI to create a connection to a provider. It contains a
createConnection() method, which returns a
Connection encapsulates an active connection to a provider. Some of its methods are:
createSession(boolean, int): Returns a
booleanparameter indicates whether the
Sessionis transacted or not; the
intindicates the acknowledgment mode (see Acknowledgment ).
start(): Activates the delivery of messages from the provider.
stop(): Temporarily stops delivery of messages; delivery can be restarted with
close(): Closes the connection to the provider and releases all resources held in its behalf.
Session is the single-threaded context for sending and receiving messages. Some of its methods are:
createProducer(Destination): Returns a
MessageProducerobject to send messages to the specified
createConsumer(Destination): Returns a
MessageConsumerobject to receive messages from the specified
commit(): Commits all consumed or produced messages for the current transaction.
rollback(): Rolls back all consumed or produced messages for the current transaction.
create<MessageType>Message(...): A variety of methods that return a
<MessageType>Message-- for example,
TextMessage, and so on.
Destination encapsulates a destination for messages. It is an administered object that is retrieved from JNDI.
MessageProducer is used to send messages. Some of its methods are:
send(Message): Sends the indicated
setDeliveryMode(int): Sets the delivery mode for subsequent messages sent; valid values are
setPriority(int): Sets the priority for subsequent messages sent; valid values are 0 through 9.
setTimeToLive(long): Sets the duration before expiration, in milliseconds, of subsequent messages sent.
MessageConsumer is used to receive messages. Some of its methods are:
receive(): Returns the next message that arrives; this method blocks until a message is available.
receive(long): Receives the next message that arrives within
longmilliseconds; this method returns
nullif no message arrives within the time limit.
receiveNoWait: Receives the next message if one is immediately available; this method returns
nullif no message is available.
setMessageListener(MessageListener): Sets the
MessageListenerobject receives messages as they arrive, that is, asynchronously (see
MessageListener is an interface with a single method --
onMessage(Message) -- that provides asynchronous receipt and processing of messages.
This interface should be implemented by a client class and an instance of that class passed to the
MessageConsumer object with the
setMessageListener(MessageListener) method. As a message arrives at a destination, it is passed to the object with the