img
As shown in Listing 16-12, the program is just like any other stand-alone Spring application. The
ApplicationContext is initialized, and then the contactService bean is retrieved. Then we just call its
methods like a local application. Running the program produces the following output:
Finding
all contacts
Contact
- Id: 1, First name: Clarence, Last name: Ho, Birthday: 1980-07-30T00:00:00.000+08:00
Contact
- Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02T00:00:00.000+08:00
Contact
- Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28T00:00:00.000+08:00
Finding contact with first name equals Clarence
Contact - Id: 1, First name: Clarence, Last name: Ho, Birthday: 1980-07-30T00:00:00.000+08:00
In the previous output, you can see the findAll() and findByFirstName() methods are called, and
the results are returned.
Using JMS in Spring
Using message-oriented middleware (generally referred to as an MQ server) is another popular way to
support communication between applications. The main benefits of a message queue (MQ) server is
that it provides an asynchronous and loosely coupled way for application integration. In the Java world,
JMS is the standard for connecting to an MQ server for sending or receive messages.
An MQ server maintains a list of queues and topics for which applications can connect to and send
and receive messages. The following is a brief description of the difference between a queue and a topic:
Queue: A queue is used to support a point-to-point message exchange model.
·
When a producer sends a message to a queue, the MQ server keeps the message
within the queue and delivers it to one and only one consumer the next time the
consumer connects.
Topic: A topic is used to support the publish-subscribe model. Any number of
·
clients can subscribe to the message within a topic. When a message arrives for
that topic, the MQ server delivers it to all clients that have subscribed to the
message. This model is particularly useful when you have multiple applications
that will be interested in the same piece of information (for example, a news feed).
In JMS, a producer connects to an MQ server and sends a message to a queue or topic. A consumer
also connects to the MQ server and listens to a queue or topics for messages of interest. In JMS 1.1, the
API was unified so the producer and consumer don't need to deal with different APIs for interacting with
queues and topics. In this section, we will focus on the point-to-point style for using queues, which is a
more commonly used pattern within an enterprise.
To develop and test a JMS application, an MQ server is required. In this section, we will use the
Apache ActiveMQ server (activemq.apache.org), which is a very popular open source MQ server.
To prepare for the sample, several new Maven dependencies are required, as listed in Table 16-2.
Please add them into your project.
Table 16-2. Maven Dependencies for JMS and ActiveMQ
Group ID
Artifact ID
Version
Description
3.1.0.RELEASE
Spring JMS module
org.springframework
spring-jms
5.5.1
ActiveMQ Java library
org.apache.activemq
activemq-core
1.1-rev-1
JMS 1.1 API
javax.jms
jms-api
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home