Client Acknowledgement Example
The following code shows the use of client acknowledgement and session recovery.
You will notice messages are not acknowledged unless you explicitly say so.
It does two passes of the messages, the first does not acknowledge, the seconds does.
The second pass is achieved by invoking
session.recover()
to force redelivery of
unacknowledged messages.
Code
import java.util.Properties; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; public class Client implements MessageListener { public static void main(String[] args) throws Exception { log.info("Creating jndi context - alternatively use a jndi.properties"); Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); properties.put(Context.PROVIDER_URL, "localhost"); InitialContext ctx = new InitialContext(properties); log.info("Looking up queue"); Queue queue = (Queue) ctx.lookup("queue/testQueue"); log.info("Looking up connection factory"); QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory"); log.info("Creating connection"); QueueConnection qc = qcf.createQueueConnection(); try { log.info("Creating session"); QueueSession qs = qc.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); log.info("Creating sender"); QueueSender sender = qs.createSender(queue); log.info("Creating messages"); TextMessage hello = qs.createTextMessage("hello"); TextMessage goodbye = qs.createTextMessage("goodbye"); log.info("Sending messages"); sender.send(hello); sender.send(goodbye); log.info("Creating receiver"); QueueReceiver receiver = qs.createReceiver(queue); log.info("You have to start the connection before receiving messages"); qc.start(); log.info("Set the message listener"); receiver.setMessageListener(new Client(qs)); log.info("Allowing the message listener 10 seconds to receive messages"); Thread.sleep(10000); } finally { qc.close(); } } QueueSession session; public Client(QueueSession session) { this.session = session; } public void onMessage(Message message) { try { TextMessage tm = (TextMessage) message; String text = tm.getText(); boolean redelivered = message.getJMSRedelivered(); log.info("Got message: " + text + " redelivered=" + redelivered); if (redelivered) { log.info("This is the second pass, do a client acknowledgment"); message.acknowledge(); return; } if (text.equals("goodbye")) { log.info("Got the last message, recover the session forcing redelivery of all messages"); session.recover(); } } catch (JMSException e) { log.error("Got unexpected error", e); System.exit(0); } } public static class log { public static void info(String message) { System.out.println(message); } public static void error(String message, Throwable t) { System.err.println(message); t.printStackTrace(); } } }
Instructions
To compile it: javac -classpath JBOSS_HOME/client/jbossall-client.jar Client.java or windows javac -classpath JBOSS_HOME\client\jbossall-client.jar Client.java To run it: java -classpath .:JBOSS_HOME/client/jbossall-client.jar Client or windows java -classpath .;JBOSS_HOME\client\jbossall-client.jar Client
Output
Creating jndi context - alternatively use a jndi.properties Looking up queue Looking up connection factory Creating connection Creating session Creating sender Creating messages Sending messages Creating receiver You have to start the connection before receiving messages Set the message listener Got message: hello redelivered=false Got message: goodbye redelivered=false Got the last message, recover the session forcing redelivery of all messages Got message: hello redelivered=true This is the second pass, do a client acknowledgment Got message: goodbye redelivered=true This is the second pass, do a client acknowledgment Allowing the message listener 10 seconds to receive messages
Things to try
Change CLIENT_ACKNOWLEDGE to AUTO_ACKNOWLEDGE
and remove the
message.acknowledge()
statement
to see the difference between client and auto acknowledgement.
Comments