Java Reference
In-Depth Information
class="org.springframework.batch.item.database.JpaPagingItemReader"
scope="step">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
<beans:property name="queryProvider">
<beans:bean
class="com.apress.springbatch.chapter7.CustomerByCityQueryProvider">
<beans:property name="cityName" value="#{jobParameters[city]}"/>
</beans:bean>
</beans:property>
</beans:bean>
Using JPA can limit an application's dependencies on third party libraries while still providing many
of the benefits of ORM libraries like Hibernate.
Up to this point you have covered file and database input sources and the variety of ways you can
obtain your input data from them. However, a common scenario concerns existing Java services that
provide the data you need. In the next section, you will cover how to obtain data from your existing Java
services.
Existing Services
Many companies have Java applications (web or otherwise) currently in production. These applications
have gone through strenuous amounts of analysis, development, testing, and bug fixing. The code that
comprises these applications is battle tested and proven to work.
So why can't you use that code in your batch processes? Let's use the example that your batch
process requires you to read in customer objects. However, instead of a Customer object mapping to a
single table or file like it has been up to now, your customer data is spread across multiple tables in
multiple databases. Also, you never physically delete customers; instead you flag them as being deleted.
A service to retrieve the customer objects already exists in your web-based application. How do you use
that in your batch process? In this section you will look at how to call existing Spring services to provide
data for your ItemReader.
Back in Chapter 4, you learned about a few adapters that Spring Batch provides for tasklets to be
able to do different things, specifically the
org.springframework.batch.core.step.tasklet.CallableTaskletAdapter ,
org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter and the
org.springframework.batch.core.step.tasklet.SystemCommandTasklet . All three of these were used to
wrap some other element in a way that Spring Batch could interact with it. To use an existing service
within Spring Batch, the same pattern is used.
In this case, you will be using the org.springframework.batch.item.adapter.ItemReaderAdapter .
This class takes two dependencies when it is configured: a reference to the service to call and the name
of the method to call. You need to keep the following two things in mind when using the
ItemReaderAdapter:
1.
The object returned from each call is the object that will be returned by the
ItemReader. If your service returns a single Customer, then that single
Customer object will be the object passed onto the ItemProcessor and finally
the ItemWriter. If a collection of Customer objects is returned by the service, it
will be passed as a single item to the ItemProcessor and ItemWriter and it will
be your responsibility to iterate over the collection.
 
Search WWH ::




Custom Search