QueueConnectionFactory qcf = (QueueConnectionFactory)
namingContext.lookup("java:comp/env/jms/myQueueConnectionFactory");
namingContext.close();
// 2. Create the queue connection
QueueConnection queueConnection = qcf.createQueueConnection();
// 3. Create the session over the queue connection.
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// 4. Create the sender to send messages over the session.
QueueSender queueSender = queueSession.createSender(null);
// When the onMessage method is called, a message has been sent.
// You can retrieve attributes of the message using the Message object.
String txt = ("mdb rcv: " + message.getJMSMessageID());
System.out.println(txt + " redel="
+ message.getJMSRedelivered() + " cnt="
+ message.getIntProperty("JMSXDeliveryCount"));
// Create a new message using the createMessage method.
// To send it back to the originator of the other message,
// set the String property of "RECIPIENT" to "CLIENT."
// The client only looks for messages with string property CLIENT.
// Copy the original message ID into new message's Correlation ID for
// tracking purposes using the setJMSCorrelationID method. Finally,
// set the destination for the message using the getJMSReplyTo method
// on the previously received message. Send the message using the
// send method on the queue sender.
// 5. Create a message using the createMessage method
Message returnMessage = queueSession.createMessage();
// 6. Set properties of the message.
returnMessage.setStringProperty("RECIPIENT", "CLIENT");
returnMessage.setIntProperty("count", message.getIntProperty("JMSXDeliveryCount"));
returnMessage.setJMSCorrelationID(message.getJMSMessageID());
// 7. Retrieve the reply destination.
Destination destination = message.getJMSReplyTo();
// 8. Send the message using the send method of the sender.
queueSender.send((Queue) destination, returnMessage);
System.out.println(txt + " snd: " + returnMessage.getJMSMessageID());
// close the connection
queueConnection.close();
} catch (Exception e) {
throw new RuntimeException("Failed to process message [" + message + "].", e);
}
}