C H A P T E R 7
■ ■ ■
In this chapter, you will learn about Spring's support for Java Message Service (JMS) . JMS defines a set of
standard APIs for message-oriented communication (using “message-oriented middleware,” a.k.a
MOM) in the Java EE platform. With JMS, different applications can communicate in a loosely coupled
way compared with other remoting technologies such as RMI. However, when using the JMS API to send
and receive messages, you have to manage the JMS resources yourself and handle the JMS API's
exceptions, which results in many lines of JMS-specific code. Spring simplifies JMS's usage with a
template-based approach, just as it does for JDBC. Moreover, Spring enables beans declared in its IoC
container to listen for JMS messages and react to them.
By the end of this chapter, you will be able to create and access message-based middleware using
Spring and JMS. This chapter will also provide you with a working knowledge of messaging in general,
which will help you in chapter 8, where we discuss Spring Integration. You will also know how to use
Spring's JMS support to simplify sending, receiving, and listening for JMS messages.
7-1. Sending and Receiving JMS Messages with Spring
In the Java EE platform, applications often need to communicate using JMS. However, to send or receive
a JMS message, you have to perform the following tasks:
• Create a JMS connection factory on a message broker.
• Create a JMS destination, which can be either a queue or a topic.
• Open a JMS connection from the connection factory.
• Obtain a JMS session from the connection.
• Send/receive the JMS message with a message producer/consumer.
• Handle JMSException , which is a checked exception that must be handled.
• Close the JMS session and connection.
As you can see, it requires a lot of coding to send or receive a simple JMS message. In fact, most of
these tasks are boilerplate and require you to repeat them each time when dealing with JMS.