Java Reference
In-Depth Information
// log errors
} finally {
if (connection != null){
To set up a consumer, call the JMS Session object's createConsumer method, and pass the destination object
that you wish to consume from. The next step is to call the JMS Connection start method. This will tell JMS that the
consumer is ready to begin receiving messages. After invoking the connection.start() method, a consumer can
receive a message by calling the Consumer object's receive method, optionally passing a time in milliseconds for the
consumer to listen for messages. If no time limit is specified, the consumer will listen indefinitely.
As you can see from the example in this recipe, once the receive method is called, a Message object is retrieved.
Once the message is received, the application can glean whatever it needs by calling the Message object's getter
methods accordingly.
Additional Information
This has been a brief overview of the JMS messaging concepts, and the use of the standard API for working with
messages. At this point, if you would like to delve deeper into these concepts, please refer to online documentation
via the Java EE 7 tutorial, or a book that covers these topics in detail. The remainder of this chapter will focus on new
features that JMS 2.0 has to offer.
API Enhancements
Prior to the release of Java EE 7, the JMS API had not been overhauled for a number of years. The newer features of the
Java platform were not being utilized to the fullest potential, and working with JMS was more complex than it needed
to be. The JMS 2.0 release brings the API up-to-date with the latest developments in Java EE, and makes it easier to
use. This section will cover some of those enhancements, including the newer simplified API, and the ability to utilize
try-with-resources, which gave the JMS API the facelift that it needed to become useful with Java 7 capabilities.
New Simple API
The reason that any application makes use of JMS is to incorporate the ability to send or receive messages. Therefore,
it is no surprise that the JMS API has been developed to make these tasks very easy for the developer. In Java EE 7,
things get even easier using the simplified JMS API.
Sending Messages
In the overview, we discussed the underlying concepts behind sending JMS messages. We also looked at how to send
messages using the standard API. Now let's take a look at how we can do the same work using the new simplified API.
To utilize the simplified API, create a JMSContext object, and then utilize it to create a MessageProducer and send the
message to the appropriate destination. In the following example, a simple String-based message is sent to a Queue
using the simplified API.
Search WWH ::

Custom Search