Home » Java » Enterprise Java » Types of JMSContext in JMS 2.0

About Abhishek Gupta

Abhishek Gupta

Types of JMSContext in JMS 2.0

If you follow Java EE, the simplified API components in JMS 2.0 (Java EE 7) will not be unknown to you. One of the important interfaces which forms a part of the simplified API is the javax.jms.JMSContext interface.

JMSContext can be categorized in to two types depending on how it’s instance is obtained and managed (life cycle) – Container managed and Application managed

Application Managed

Let’s look at a code snippet:

@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");
        }
    }
}

In this case, an instance of JMSContext was obtained using ConnectionFactory and hence needs to be closed by the application code. The container will not manage it’s lifecycle

Container Managed

If you use @Inject to allow the container to inject the JMSContext instance, the container assumes full responsibility for it’s lifecycle.

@Path("email")
@Stateless
public class AwesomeEmailService {
    
    //container managed
    @Inject
    JMSContext ctx;
 
    @Resource("jms/emailQ")
    Destination emailQ;
 
    @POST
    public void send(String email) {
        Session session;
        try {
            ctx.createProducer().send(emailQ, email);
            System.out.println("Message Sent to queue - " + ((Queue) emailQ).getQueueName());
        } catch (JMSException ex) {
            Logger.getLogger(AwesomeEmailService.class.getName()).log(Level.SEVERE, null, ex);
            throw new JMSRuntimeException(ex.getMessage(), ex.getMessage(), ex);
        }
    }
}

If you call close() on a container managed JMSContext, you will end up with this exception – javax.jms.IllegalStateRuntimeException: This method is not permitted on a container-managed (injected) JMSContext.

And….

In the previous example, the container automatically uses the default Connection Factory (as per Java EE 7). You can also leverage @JMSConnectionFactory annotation (another useful addition to JMS 2.0) to specify a Connection Factory of your choice.

@Path("email")
@Stateless
public class TheBestEmailService {
 
    //container managed
    @Inject
    @JMSConnectionFactory("jms/myConnectionFactory")
    JMSContext ctx;
 
    @Resource("jms/emailQ")
    Destination emailQ;
 
    @POST
    public void send(String email) {
        Session session;
        try {
            ctx.createProducer().send(emailQ, email);
            System.out.println("Message Sent to queue - " + ((Queue) emailQ).getQueueName());
        } catch (JMSException ex) {
            Logger.getLogger(TheBestEmailService.class.getName()).log(Level.SEVERE, null, ex);
            throw new JMSRuntimeException(ex.getMessage(), ex.getMessage(), ex);
        }
    }
}

That’s it for now.. Cheers !

Reference: Types of JMSContext in JMS 2.0 from our JCG partner Abhishek Gupta at the Object Oriented.. blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of