Java Messaging Services
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.
- Java Message Service (JMS)
- Anyone looking to get Oracle Java Certification
- Those who want to improve Java 7 EE knowledge
- Java developers
- Have at least 2 years of Java development experience
Hello there. In this lesson, we will talk about writing message consumers. So, let's start. A JMS Consumer is used to receive messages from a queue or topic. It is created with one of the create consumer methods on a JMS Context by parsing a queue or a topic. As you will see later, a JMS Consumer can be created with a message selector so it can restrict messages delivered. A client may receive a message synchronously or asynchronously as they arrive. For asynchronous delivery, a client can register a MessageListener object with a JMS Consumer. As messages arrive, the provider delivers them by calling the MessageListeners on message method. The JMS Consumer interface has also a lot of methods like the producer interface. Let's examine the methods of the JMS Consumer interface.
Close method closes the JMS Consumer. Receive method receives the next message produced. If you use the receive method with a time parameter, the method receives the next message that arrives within a specified time out interval. Receive body receives the next message produced and returns its body as an object of the specified type. If you add a time parameter to this method, this method receives the next message produced for this JMS Consumer that arrives within the specified timeout period and returns its body as an object of the specified type. receiveNoWait receives the next message if one is immediately available. receiveBodyNoWait receives the next message produced for this JMS Consumer if one is immediately available and returns its body as an object of the specified type. setMessageListener sets the message listener and getMessageListener gets the message listener.
String getMessageSelector gets the message selector expression. This method must not be used in a java EE web or EJB application. Doing so may cause a JMS Runtime exception to be thrown. getMessageSelector gets this JMS Consumers message selector expression. A client uses a JMS Consumer to receive messages from a destination. A JMS Consumer is created by passing a queue or topic to the JMS Context to createConsumer method. Messaging is inherently asynchronous and that there is no timing dependency between producers and consumers. However, the client itself can consume messages in two ways: synchronously and asynchronously. In the synchronous way, a receiver explicitly fetches the message from the destination by calling the received method. In an asynchronous way, a receiver decides to register to an event that is triggered when the message arrives. It has to implement the MessageListener interface, and whenever a message arrives, the provider delivers it by calling the onMessage method.
This figure illustrates these two types of approaches. Let's talk about the delivery operations. I will start with synchronous delivery. A synchronous consumer needs to start a JMS context loop until a new message arrives and request the message using one of its receive methods. There are several variations of receive that allow a client to pull or wait for the next message. The following steps explain how you can create a synchronous consumer that consumes a message from a queue; obtain a connection factory and a topic using JNDI look ups or injection, create a JMS context object using the connection factory, create a javax.jms.JMSConsumer using the JMS Context object. Loop and call the receive method or in this case receive body on the consumer object.
The receive methods block if the queue is empty and wait for a message to arrive. Here, the infinite loop waits for other messages to arrive. Let's take a look at this code snippet. As you can see, factory or destination steps are the same as those of the producer. In try, we will create our context. And after that, with createConsumer, we will create our message. And after that, with receiveBody, we can receive the next message produced and return its body as an object of the specified type. Done. Now let's talk about asynchronous delivery. Asynchronous consumption is based on event handling. A client can register an object including itself that implements the MessageListener interface. A MessageListener is an object that acts as an asynchronous event handler for messages. As messages arrive, the provider delivers them by calling the listeners on message method, which takes one argument of type message. With this event model, the consumer doesn't need to loop indefinitely to receive a message.
MDBs use this event model. To create an asynchronous messageListener, we have to follow five steps. The class implements the javax.jms.jMS.MessageListener interface, which defines a single method called on-message. Obtain a connection factory and a topic using JNDI look ups. Create a javax.jms.JMSConsumer using the JMS Context object. Call the setMessageListener method parsing an instance of a messageListener interface. Implement the onMessage method and process the receive message. Each time a message arrives, the provider will invoke this method, parsing the message. Let's take a look at this code snippet. Let's check the steps. First, implements a MessageListener, We have a ConnectionFactory and other components, we have a JMS Consumer, we also have a setMessageListener method, and the last step is the onMessage method that we have used in this code snippet. With the onMessage method, we can show a text in the console. Now, I think that's enough for JMS Consumer. We have now finished looking at the consumer. In the next video, we will examine the reliability mechanisms. 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.