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
To run the example, simply type mvn verify -Pexample
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
class MySendAcknowledgementsHandler implements SendAcknowledgementHandler
{
int count = 0;
public void sendAcknowledged(final Message message)
{
System.out.println("Received send acknowledgement for message " + count++);
}
}
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
ClientSession coreSession = ((ActiveMQSession)session).getCoreSession();
coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
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);
}
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();
}
}