In this section, we’ll look at each of the important JMS interfaces for point-to-point programming and some of their methods.We’ll look at some sample code that performs point-to-point message processing.
QueueConnectionFactory is an administered object that is retrieved from JNDI to create a connection to a provider. It contains a createQueueConnection() method which returns a QueueConnection object.
QueueConnection encapsulates an active connection to a provider. Some of its methods are:
* createQueueSession(boolean, int): Returns a QueueSession object. The boolean parameter indicates whether the QueueSession is transacted or not; the int indicates the acknowledgement mode (see Acknowledgement on page 12 ).
* start() (inherited from Connection): Activates the delivery of messages from the provider.
* stop() (inherited from Connection): Temporarily stops delivery of messages; delivery can be restarted with start().
* close() (inherited from Connection): Closes the connection to the provider and releases all resources held in its behalf.
QueueSession is the single-threaded context for sending and receiving PTP messages. Some of its methods are:
* createSender(Queue): Returns a QueueSender object to send messages to the specified Queue.
* createReceiver(Queue): Returns a QueueReceiver object to receive messages from the specified Queue.
* createBrowser(Queue): Returns a QueueBrowser object to browse messages on the specified Queue.
* commit() (inherited from Session): Commits all consumed or produced messages for the current transaction.
* rollback() (inherited from Session): Rolls back all consumed or produced messages for the current transaction.
* create<MessageType>Message(…) (inherited from Session): A variety of methods that return a <MessageType>Message, for example, MapMessage,TextMessage, and so on.
Queue encapsulates a point-to-point destination. It is an administered object that is retrieved from JNDI.
QueueSender is used to send point-to-point messages. Some of its methods are:
* send(Message): Sends the indicated Message.
* setDeliveryMode(int) (inherited from MessageProducer): Sets the delivery mode for subsequent messages sent; valid values are DeliveryMode.PERSISTENT and DeliveryMode.NON_PERSISTENT.
* setPriority(int) (inherited from MessageProducer): Sets the priority for subsequent messages sent; valid values are 0 through 9.
* setTimeToLive(long) (inherited from MessageProducer): Sets the duration before expiration, in milliseconds, of subsequent messages sent.
QueueReceiver is used to receive point-to-point messages. Some of its methods are:
* receive() (inherited from MessageConsumer): Returns the next message that arrives; this method blocks until a message is available.
* receive(long) (inherited from MessageConsumer): Receives the next message that arrives within long milliseconds; this method returns null if no message arrives within the time limit.
* receiveNoWait (inherited from MessageConsumer): Receives the next message if one is immediately available; this method returns null if no message is available.
* setMessageListener(MessageListener) (inherited from MessageConsumer): Sets the MessageListener; the MessageListener object receives messages as they arrive, that is, asynchronously (see MessageListener on page 16).
When using QueueReceiver to receive messages, the messages are removed from the queue when they are received. QueueBrowser is used to look at messages on a queue without removing them. The method for doing that is getEnumeration(), which returns a java.util.Enumeration that can be used to scan the messages in the queue; changes to the queue (arriving and expiring of messages) may or may not be visible.
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 QueueReceiver object with the setMessageListener(MessageListener) method. As a message arrives on a queue, it is passed to the object by calling the onMessage(Message) method.
MessageListener objects are used in both the PTP and pub/sub domains.