Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Last active August 29, 2015 14:23
Show Gist options
  • Save abhirockzz/6e139e404fd6e3db1506 to your computer and use it in GitHub Desktop.
Save abhirockzz/6e139e404fd6e3db1506 to your computer and use it in GitHub Desktop.
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