Java Message Service Concepts
The course is part of this learning path
This course covers the Java Message Service (JMS) API and its main concepts. Finally, we will look through example questions.
- 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
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 consumers. So, let's start. 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 contexts createConsumer() method. Messaging is inherently asynchronous in that there is no timing dependency between producers and consumers. However, the client itself can consume messages in two ways: Synchronously, a receiver explicitly fetches the message from the destination by calling the receive method. Asynchronously, 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 consumer. A synchronous consumer needs to start a JMS context, loop to wait until a new message arrives and request to the arrived 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 lookups or injection. Create a JMSContext object using the connection factory. Create a javax.jms.JMSConsumer using the JMSContext 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 the sample code: try. Looks up the administered objects. 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 onMessage 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. The following steps describe the process used to create an asynchronous message listener. The class implements the javax.jms.MessageListener interface, which defines a single method called onMessage. Obtain a connection factory and a topic using JNDI lookups or injection. Create a javax.jms.JMSConsumer using the JMS context object.
Call the setMessageListener() method, passing an instance of a MessageListener interface. Implement the onMessage() method and process the received message. Each time a message arrives, the provider will invoke this method passing the method. Let's take a look at this sample code: public class Listener implements MessageListener; try gets the jndiContext. Looks up the administered objects. Try JMSContext equal. Catch (NamingException). System.out.println("Async Message received"). So, that's it. Hope to see you in our next lesson. Have a nice day.
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.