Asynchronous Send Acknowledgements are an advanced feature of JBoss Messaging which allow you to receive acknowledgements that messages were successfully received at the server in a separate stream to the stream of messages being sent to the server.
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
the server calls the handler when it has successfully received each message.
For more information on Asynchronous Send Acknowledgements please see the user manual
To run the example, simply type ant
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 = ((JBossSession)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();
}
}