Writing Message Producer
Start course
1h 24m

This course covers the Java Message Service (JMS) API and its main concepts. Finally, we will look through example questions.

Learning Objectives

  • Understand the fundamentals of the JMS API and messaging
  • Learn about writing message producers and consumers
  • Learn about reliability mechanisms
  • Learn how to write message-driven beans

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.


Basic knowledge of Java programming.


Hello there, in this lesson, we'll talk about Writing Message Producers.

So, let's start. The new JMS simplified API allows you to write producers and consumers in a less verbose manner than with the classic API. But it still needs both of the administered objects, connection factory, and destination. Depending if you are running outside or inside a container, EJB, web or ACC, you will either use JNDI lookups or injection. As you've seen previously, the JMS context API is the central API to produce and consume messages. If your application runs outside a container, you will need to manage the lifecycle of the JMS context by creating and closing it programmatically. If you run inside a container, you can just inject it and leave the container to manage its lifecycle.

A message producer, JMS producer, is an object created by the JMS context and is used to send messages to a destination. Let's try to explain how to create a producer that sends a message to a queue in a container in a pure JavaSc environment. Obtain a connection factory and a queue using JNDI lookups. Create a JMS context object using the connection factory. Notice the try-with resources statement that will automatically close the JMS context object. Create a Javax.jms.JMSProducer using the JMS context object. 

Send a text message to the queue using the JMSProducer.send method. If you compare this code to our previous codes, you'll notice that the code is less for both. Exception handling is also neater, as the new JMS runtime exception is used in the new API and is an unchecked exception. Connection factories and destinations are administered objects that reside in a message provider and have to be declared in the JNDI name space, which is why you use the JNDI API to look them up.

When the client code runs inside a container, Dependency Injection can be used instead. JavaEE seven has several containers, EJB, Serverless, and Application Client Container, ACC. If the code runs in one of these containers, the @Resource annotation can be used to inject a reference to that resource by the container. With JavaEE seven, using resources is much easier as you don't have the complexity of JNDI, or are not required to configure resource references in deployment descriptors. You can just rely on the container injection capabilities. To use the @Resource annotation, let's take the example of the producer in this code snippet. Change it to a stateless session bin and use injection instead of JNDI look-ups. In this code snippet, both the connection factory and the queue are looked up using JNDI. In this code, the JNDI name is on the @Resource annotation.

When the producer EJB runs in a container, references of connection factory and queue are injected at initialization. When the producer is executed in a container, EJB or serverless container with CDI enabled, it can inject the JMS context. The container will then manage its lifecycle. No need to create or close the JMS context. This can be done, thanks to the @Inject and @JMSConnectionFactory annotations. The annotation, JavaX.JMS.JMSConnectionFactory may be used to specify the JNDI look-up name of the connection factory used to create the JMS context. If the JMSConnectionFactory annotation is omitted, then the platform default JMSConnectionFactory will be used. Let's look at this code. This code is quite minimalist. The container does all the work of injecting the needed components and managing their life cycle. As a developer, you just need one line of code to send a message. The annotation Javax.JMS.JMSPasswordCredential can also be used to specify a username and password for when the JMS context is created. So, that's it, hope to see you in our next lesson. Have a nice day.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics