Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Application Managed JMS Context
@Path("email")
@Stateless
public class EmailService {
//pulls in default Conn Factory as per Java EE 7
@Resource
ConnectionFactory cf;
//application managed
JMSContext ctx;
@Resource("jms/emailQ")
Destination emailQ;
@POST
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
ctx.close();
System.out.println("JMSContext closed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment