Automated provisioning of JMS resources in Java EE 7

JMS 2.0 (part of the Java EE 7 Platform) introduced lots of nice features. One of these was the ability to declare JMS resources for automatic deployment.

Pre Java EE 7

  • Inject Connection Factory using @Resource
  • Lookup Destination (Queue/Topic) using @Resource
  • Pull out the Session object and use it to create the Message, Message Producer and send the message

Most importantly, you had to make sure that the resources i.e. the Connection Factory and the physical destinations were configured in your application server in advance

In the Java EE 7 era ….

You can leverage JMS 2.0 goodies

  • Use injected JMS Context (in most of the cases) to ease the sending process with less boilerplate code
  • Most importantly, you can declaratively configure auto provisioning of JMS Resources using annotations or deployment descriptors

Let’s look at the new JMS 2.0 annotations in action. You can also pick up this Maven project on Github and deploy it in your favourite IDE

@JMSConnectionFactoryDefinition, @JMSConnectionFactoryDefinitions

Used to declare one or more connection factories

@JMSDestinationDefinition, @JMSDestinationDefinitions

Used to declare one or more physical destinations (queues or topics)

Oh and you can also use XML

These can be a part of the web deployment descriptor (web.xml) or the EJB deployment descriptor (ejb-jar.xml)

Possible variations

There are several ways to use this feature

  • Declare your JMS resources using a @Startup powered @Singleton EJB
  • You can also declare it on a Servlet or any CDI managed bean for that matter

What’s the point of all this ?

The container/Java EE application server makes sure that the JMS artefacts are available to your application logic on-demand

  • It’s valuable in PaaS, microservices, dockerized and any other environment which heavily leverage automated deployments
  • Good for automated testing
  • It’s one less item to think about and configure!

Additional resources


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: Logo

You are commenting using your 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