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

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.

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.


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.

That’s it for now.. Cheers !

About Abhishek

Currently working as a Senior Product Manager in the Oracle Cloud Application Development team with a focus on Oracle Cloud PaaS portfolio. When not hovering in the clouds, I stay grounded with Java EE
This entry was posted in Java, Java EE and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s