Java Reference
In-Depth Information
In previous examples in this chapter, you have had a single step that read in the customer.csv file
and wrote it out using the appropriate ItemWriter for the example. For this example, however, that won't
be enough. If you read in the items and write them to the JMS queue, you won't know that everything got
onto the queue correctly because you can't see what is in the queue. Instead, as Figure 9-5 shows, you
use two steps for this job. The first one reads the customer.csv file and writes it to the ActiveMQ queue.
The second step reads from the queue and writes the records out to an XML file.
customer.csv
Step 1
customerQueue
Step 2
customer.xml
Figure 9-5. Processing for jmsFormatJob
It's important to note that you don't want to do this in an actual production environment because a
message isn't pulled off the queue until all of them have been put on it. This could lead to running out of
room in your queue depending on how it's configured and the resources available. However, for this
example and given the small number of customers you're processing, this approach demonstrates the
point.
To begin using org.springframework.batch.item.jms.JmsItemWriter , you need to configure a
couple of JMS-related beans in launch-context.xml . Luckily, Spring makes this very easy. 4 You need to
configure three beans:
A queueI This is the destination for JmsItemWriter . It's a queue provided by
ActiveMQ.
A connection factory: The job needs to be able to obtain a connection to the queue
(similar to a connection to a database).
A JmsTemplate : This is the Spring component that is does all the heavy lifting for
you.
Let's start by looking at the queue. Although ActiveMQ offers a number of options for configuring a
queue, it makes things simple to get up and running by allowing you to configure a JMS queue via
Spring. You will configure the queue to be dynamically created on startup and serve as the destination.
With the queue itself configured, you can configure the connection factory to access it. Just like the
queue itself, ActiveMQ exposes a class that allows you to configure the connection factory via Spring. To
do that, all you need to do is define a URL in which the connection factory can find the broker. In this
case, you're telling it to look at the local JVM.
Finally you can create JmsTemplate . This is Spring's way of exposing JMS functionality in a way that's
easy to use and understand. To put it to use here, you need to provide three dependencies: a reference to
the connection factory, a reference to the queue, and a timeout value for how long the reader will wait
when listening for messages. Listing 9-48 shows the configuration of the JMS resources in launch-
context.xml .
Listing 9-48. JMS Resource Configuration in launch-context.xml
4 Entire topics have been devoted to the subject of JMS, not to mention volumes on the topic of Spring
integrating with JMS. This topic keeps things simple to emphasize the integration of Spring Batch and
JMS. For more information on Spring and JMS, check out Pro Spring Integration (Apress, 2011).
 
Search WWH ::




Custom Search