Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Application Managed JMS Context
public class EmailService {
//pulls in default Conn Factory as per Java EE 7
ConnectionFactory cf;
//application managed
JMSContext ctx;
Destination emailQ;
public void send(String email) {
Session session;
try {
ctx = cf.createContext();
ctx.createProducer().send(emailQ, email);
System.out.println("Message Sent to queue - " + ((Queue) emailQ).getQueueName());
} catch (JMSException ex) {
Logger.getLogger(EmailService.class.getName()).log(Level.SEVERE, null, ex);
throw new JMSRuntimeException(ex.getMessage(), ex.getMessage(), ex);
} finally {
//clean up after use. Can also be done as inside a @PreDestroy callback method
System.out.println("JMSContext closed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment