Last active
August 29, 2015 14:23
-
-
Save abhirockzz/6e139e404fd6e3db1506 to your computer and use it in GitHub Desktop.
Application Managed JMS Context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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