# Asynchronous Send Acknowledgements Example

    To run the example, simply type mvn verify from this directory,
    or mvn -PnoServer verify if you want to start and create the server manually.

Asynchronous Send Acknowledgements are an advanced feature of ActiveMQ Artemis 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 -Pexample` 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`

               InitialContext initialContext = getContext();

2. We look-up the JMS queue object from JNDI

               Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");

3. We look-up the JMS connection factory object from JNDI

               ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");

4. We create a JMS connection

               connection = cf.createConnection();

5. Define a SendAcknowledgementHandler which will receive asynchronous acknowledgements

             class MySendAcknowledgementsHandler implements SendAcknowledgementHandler
             {
                int count = 0;

                public void sendAcknowledged(final Message message)
                {
                   System.out.println("Received send acknowledgement for message " + count++);
                }
             }

6. Create a JMS session

              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

7. Set the handler on the underlying core session

             ClientSession coreSession = ((ActiveMQSession)session).getCoreSession();

             coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());

8. Create a JMS Message Producer

             MessageProducer producer = session.createProducer(queue);

             producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

9. Send 5000 messages, the handler will get called asynchronously some time later after the messages are sent.

             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);
             }

10. 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

               finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }