Sending and Receiving Messages
Start course
1h 32m

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


  • Have at least 2 years of Java development experience 

Hello there. In this lesson, we will talk about sending and receiving messages in the JMS concept. So, let's start. With the simplified API, you need several artifacts to be able to send and receive messages or produce and consume a message. These are; ConnectionFactory, Connection, Session, et cetera. On top of that, you also need to deal with the JMSException, which is a checked exception. This API was created with JMS 1.1 in 2002 and was not changed until JMS 2.0. JMS 2.0 introduces a new simplified API which consists mainly of three new interfaces that we will examine. These interfaces rely internally on the ConnectionFactory and other classic APIs but leave the boilerplate code aside. Thanks to the new JMSRuntimeException, which is an unchecked exception, the code to send or receive a message is now much easier to write and read. Here, this figure shows a simplified class diagram of this new API. But the important point is the Legacy, Classic and simplified APIs are all under the javaX.JMS package. The simplified API provides the same messaging functionality as the classic API but requires fewer interfaces and is simpler to use.

These main interfaces are; JMS Context, JMS Producer and JMS Consumer. JMS Context is an active connection to a JMS Provider and a single threaded context for sending and receiving messages. JMS Producer is an object created by a JMS Context that is used for sending messages to a queue or topic. JMS Consumer is an object created by a JMS Context that is used for receiving messages sent to a queue or topic. In this video, we will examine the JMS Context and in the following video, we will examine the others. Let's start with JMS Context. The JMS Context is the main interface in the simplified JMS API introduced by JMS 2.0. It combines the functionality of two separate objects from the JMS 1.1 classic API, a connection, the physical link to the JMS Provider and a session, a single threaded context for sending and receiving messages. A JMS Context may be created by the application by calling one of several create context methods on a connection factory and then closed. Alternatively, if the application is running in a container EJB or web, the JMS Context can be injected using the add inject annotation container managed.

When an application needs to send messages, it uses the createProducer method to create a JMS Producer which provides methods to configure and send messages. Messages may be sent either synchronously or asynchronously. To receive messages, an application can use one of several createConsumer methods to create a JMS Consumer. This table shows a subset of the JMS Context API. Start(); starts or restarts delivery of incoming messages. Stop(); temporarily stops the delivery of incoming messages. Close method; closes the JMS Context. Commit method; commits all messages done in this transaction and releases any locks currently held. Rollback method; rolls back any messages done in this transaction and releases any locks currently held. As you know, createBytesMessage method, creates a bytes message object. The other create method is the same as with the createByteMessage.

For example, for map messages, you will use the createMapMessage method or for create message, you will need the createMessage method. Let's look at the others. CreateTopic; creates a topic object with a name parameter. This means you have to send the topic name as a string parameter. CreateQueue; creates a queue object like the createTopic method. CreateConsumer; creates a JMS Consumer for the specified destination. If you want, you can also add a message selector parameter to the createConsumer method, CreateProducer; creates a new JMS Producer object that can be used to configure and send messages. CreateContext; creates a new JMS Context with the specified session mode. I think that's enough for this video. In the next video, we will continue with the JMS Producer interface. See you in the next video.


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