Asynchronous Send Acknowledgements Example

Asynchronous Send Acknowledgements are an advanced feature of ActiveMQ which allow you to receive acknowledgements that messages were successfully received at the server in a separate thread to the sending thread

In this example we create a normal JMS session, then set a SendAcknowledgementHandler on the JMS session's underlying core session. We send many messages to the server without blocking and asynchronously receive send acknowledgements via the SendAcknowledgementHandler.

For more information on Asynchronous Send Acknowledgements please see the user manual

Example step-by-step

To run the example, simply type mvn verify from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            InitialContext initialContext = getContext();
            
  3. We look-up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We look-up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. Define a SendAcknowledgementHandler which will receive asynchronous acknowledgements
  10.            
             class MySendAcknowledgementsHandler implements SendAcknowledgementHandler
             {
                int count = 0;
                
                public void sendAcknowledged(final Message message)
                {
                   System.out.println("Received send acknowledgement for message " + count++);
                }            
             }           
               
            
  11. Create a JMS session
  12.           Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
  13. Set the handler on the underlying core session
  14.            
             ClientSession coreSession = ((ActiveMQSession)session).getCoreSession();
             
             coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());
               
               
            
  15. Create a JMS Message Producer
  16.            
             MessageProducer producer = session.createProducer(queue);
             
             producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);           
               
            
  17. Send 5000 messages, the handler will get called asynchronously some time later after the messages are sent.
  18.            
             final int numMessages = 5000;
             
             for (int i = 0; i < numMessages; i++)
             {
                javax.jms.Message jmsMessage = session.createMessage();
                
                producer.send(jmsMessage);
                
                System.out.println("Sent message " + i);
             }           
               
            
  19. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  20.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }