Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automated Provisioning of JMS Resources in Java EE 7

DZone's Guide to

Automated Provisioning of JMS Resources in Java EE 7

See exactly what has changed in Java EE 7 that allows JMS resources to be automatically deployed.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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).

@Stateless
@JMSConnectionFactoryDefinition(name = "java:comp/env/AutoDeloyedJMSConf")
@JMSDestinationDefinition(interfaceName = "javax.jms.Queue", name  = "java:comp/env/AutoDeloyedJMSQueue")
public class Service {
    //usage omitted...
}

@JMSConnectionFactoryDefinitions({
    @JMSConnectionFactoryDefinition(
       name="java:comp/env/AutoDeloyedJMSConf1"
    ),
    @JMSConnectionFactoryDefinition(
       name="java:comp/env/AutoDeloyedJMSConf2"
    ) 
})
@JMSDestinationDefinitions({
    @JMSDestinationDefinition(
       name="java:comp/env/AutoDeloyedJMSQueue1",
       interfaceName = "javax.jms.Queue",
    ),
    @JMSDestinationDefinition(
       name="java:comp/env/AutoDeloyedJMSQueue2",
       interfaceName = "javax.jms.Queue",
    ) 
})
@Stateless
public class AnotherService {
    //usage omitted...
}

Oh and you can also use XML:

//snippet only..

<jms-connection-factory>
   <name>java:comp/env/AutoDeloyedJMSConf3</name>
</jms-connection-factory>

<jms-destination>
   <name>java:comp/env/AutoDeloyedJMSQueue3</name>
   <interfaceName>javax.jms.Queue</interfaceName>
</jms-destination>

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
@Singleton
@Startup
@JMSDestinationDefinition(interfaceName = "javax.jms.Queue", name  = "java:comp/env/EmailQueue")
public class EmailQueueAutoProvisionService {

    @PostConstruct
    public void confirm(){
        System.out.println("Email Queue configured");
    }
}

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


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ee 7 ,jms

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}