For more flexibility, Message Queue lets you customize the JMS client-acknowledge mode. In client-acknowledge mode, the client explicitly acknowledges message consumption by invoking the acknowledge() method of a message object. The standard behavior of this method is to cause the session to acknowledge all messages that have been consumed by any consumer in the session since the last time the method was invoked. (That is, the session acknowledges the current message and all previously unacknowledged messages, regardless of who consumed them.)
In addition to the standard behavior specified by JMS, Message Queue lets you use client-acknowledge mode to acknowledge one message at a time.
Observe the following rules when implementing custom client acknowledgment:
To acknowledge an individual message, call the acknowledgeThisMessage() method. To acknowledge all messages consumed so far, call the acknowledgeUpThroughThisMessage() method. Both are shown in the following code example.
public interface com.sun.messaging.jms.Message { void acknowledgeThisMessage() throws JMSException; void acknowledgeUpThroughThisMessage() throws JMSException; }
When you compile the resulting code, include both imq.jar and jms.jar in the class path.
Don’t call acknowledge(), acknowledgeThisMessage() , or acknowledgeUpThroughThisMessage() in any session except one that uses client-acknowledge mode. Otherwise, the method call is ignored.
Don’t use custom acknowledgment in transacted sessions. A transacted session defines a specific way to have messages acknowledged.
If a broker fails, any message that was not acknowledged successfully (that is, any message whose acknowledgment ended in a JMSException) is held by the broker for delivery to subsequent clients.
Example 3–6 demonstrates both types of custom client acknowledgment.
... import javax.jms.*; ...[Look up a connection factory and create a connection.] Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); ...[Create a consumer and receive messages.] Message message1 = consumer.receive(); Message message2 = consumer.receive(); Message message3 = consumer.receive(); ...[Process messages.] ...[Acknowledge one individual message. Notice that the following acknowledges only message 2.] ((com.sun.messaging.jms.Message)message2).acknowledgeThisMessage(); ...[Continue. Receive and process more messages.] Message message4 = consumer.receive(); Message message5 = consumer.receive(); Message message6 = consumer.receive(); ...[Acknowledge all messages up through message 4. Notice that this acknowledges messages 1, 3, and 4, because message 2 was acknowledged earlier.] ((com.sun.messaging.jms.Message)message4).acknowledgeUpThroughThisMessage(); ...[Continue. Finally, acknowledge all messages consumed in the session. Notice that this acknowledges all remaining consumed messages, that is, messages 5 and 6, because this is the standard behavior of the JMS API.] message5.acknowledge(); |