use of javax.jms.JMSProducer in project javaee7-samples by javaee-samples.
the class MessageSenderAsync method sendMessage.
/**
* Send a message to the JMS queue. Prin
*
* @param message the contents of the message.
* @throws JMSRuntimeException if an error occurs in accessing the queue.
*/
public void sendMessage(String message) throws JMSRuntimeException {
JMSProducer producer = context.createProducer();
try {
producer.setAsync(new CompletionListener() {
@Override
public void onCompletion(Message msg) {
try {
System.out.println(msg.getBody(String.class));
} catch (JMSException ex) {
Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void onException(Message msg, Exception e) {
try {
System.out.println(msg.getBody(String.class));
} catch (JMSException ex) {
Logger.getLogger(MessageSenderAsync.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (JMSRuntimeException ex) {
System.out.println("Caught RuntimeException trying to invoke setAsync - not permitted in Java EE. Resorting to synchronous sending...");
}
producer.send(asyncQueue, message);
}
Aggregations