The course is part of this learning path
In this course, we will learn the concepts of Java EE 7 with a focus on Java Message Service.
Learning Objectives
- Java Message Service (JMS)
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to improve Java 7 EE knowledge
- Java developers
Prerequisites
- Have at least 2 years of Java development experience
Hello there. In this video, we will talk about writing message producers, so let's start. A JMSProducer is used to send messages on behalf of a JMSContext. It provides various send methods to send a message to a specified destination. An instance of JMSProducer is created by calling the createProducer method on a JMSContext. It also provides methods to allow send options, message properties, and message headers to be specified prior to sending the message. The JMSProducer interface has a lot of methods. I can't explain them all. I will talk about the most important methods in the JMSProducer interface. ClearProperties method: clears any message property set on this JMS producer. GetAsync method. If subsequent calls to send on this JMS producer object have been configured to be asynchronous, then the getAsync method returns the completion listener that has previously been configured. GetDeliveryMode: returns the delivery mode of messages that are sent using this JMSProducer. GetDisabledMessageID: gets an indication of whether message IDs are disabled. GetJMSType: returns the JMSType header value that has been set on this JMSProducer.
GetPropertyNames: returns an unmodifiable set view of the names of all the message properties that have been set on this JMSProducer. Property exists with string name parameter indicates whether a message property with the specified name has been set on this JMSProducer. We have five send methods with different parameters. The destination parameter is a parameter for all these five send methods. Let's continue with set methods. SetDeliveryDelay: sets the minimum length of time in milliseconds that must elapse after a message is sent before the JMSProvider may deliver the message to a consumer. SetJMSType: specifies that messages sent using this JMSProducer will have their JMSType header value set to the specified message type. SetProperty: specifies that messages sent using this JMSProducer will have the specified property set to the specified parameter type value. We can use this method with nine different parameter types, such as byte, double, float, int, etc. 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, connectionFactory and destination.
Depending on if you are running outside or inside a container, like EJB, Web, or ACC, you will either use JNDI lookups or injection. As you've seen previously, the JMSContext API is the central API to produce and consume messages. If your application runs outside a container, you'll need to manage the lifecycle of the JMSContext 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. Now, let's look at producing a message outside a container. A message producer, JMSProducer, is an object created by the JMSContext 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 of any container in a pure JavaSC environment. This will consist of four steps. Obtain a connection factory and a queue using JNDI lookups. Create a JMSContext object using the connection factory. If you use the try with resources statement, that will automatically close JMSContext object. Create a javax.jms.JMSProducer using the JMSContext object. Send a text message to the queue using the JMSProducer.send method. Let's examine this code snippet.
As you can see, the connection factory or destination codes are the same as before. In try, we will create our context. As I said before, this will provide us the automatic close operation. Exception handling is also neater, as the new JMSRuntime exception is used in the new API and is an unchecked exception. Now, let's continue with producing a message inside a container. Connection factories and destinations are administered objects that reside in a message provider and have to be declared in the JNDI namespace, 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. Java EE7 has several containers, like EJB, servlets, and the Application Client Container, known as 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 Java EE7, using resources is much easier, as you don't have the complexity of JNDI and are not required to configure resource references in deployment descriptors. You just rely on the container's 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 bean and use injection instead of JNDI lookups. 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 a container, references of connection factory and queue are injected at initialization. Done. Now, let's look at producing a message inside a container with context and dependency. Injection also known as CDI. When the producer is executed in a container, like an EJB or servlet container with CDI enabled, it can inject the JMSContext. The container will then manage its lifecycle. You don't need to create or close the JMSContext. We can do this by using the @Inject and @JMSConnectionFactory annotations.
The annotation javax.JMS.JMSConnectionFactory may be used to specify the JNDI lookup name of the connection factory used to create the JMSContext. If the JMConnectionFactory annotation is omitted, then the platform's default JMSConnectionFactory will be used. Let's examine this code snippet. This code is quite minimalist. The container does all the work of injecting the needed components and managing their lifecycle. As a developer, you just need one line of code to send a message. Or let's look at this code snippet. The annotation javax.JMS.JMSPasswordCredential can also be used to specify a username and password for when the JMSContext is created. So that's it. I think the JMSProducer is now clear. In the next video, we will examine the JMSConsumer interface. See you in the next video.
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.