Java Reference
In-Depth Information
configuration of the
CallableTaskletAdapter
bean shown in Listing 4-24,
CallableTaskletAdapter
contains a single dependency: the callable object itself.
Listing 4-24.
Using
CallableTaskletAdapter
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="
http://www.springframework.org/schema/batch"
xmlns:beans="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
<beans:import resource="../launch-context.xml"/>
<beans:bean id="callableObject"
class="com.apress.springbatch.chapter4.CallableLogger"/>
<beans:bean id="callableTaskletAdapter"
class="org.springframework.batch.core.step.tasklet.CallableTaskletAdapter">
<beans:property name="callable" ref="callableObject"/>
</beans:bean>
<job id="callableJob">
<step id="step1">
<tasklet ref="callableTaskletAdapter"/>
</step>
</job>
</beans:beans>
One thing to note with
CallableTaskletAdapter
is that although the tasklet is executed in a different
thread than the step itself, this doesn't parallelize your step execution. The execution of this step won't
be considered complete until the
Callable
object returns a valid
RepeatStatus
object. Until this step is
considered complete, no other steps in the flow in which this step is configured will execute. You see
how to parallelize processing in a number of ways, including executing steps in parallel, later in this
book.
MethodInvokingTaskletAdapter
The next
Tasklet
implementation is
org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter
. This class is similar to a
number of utility classes available in the Spring framework. It allows you to execute a preexisting
method on another class as the step of your job. Say for example you already have a service that does a
piece of logic that you want to run once in your batch job. Instead of writing an implementation of the
Tasklet
interface that really just wraps that method call, you can use
MethodInvokingTaskletAdapter
to
call the method. Listing 4-25 shows an example of the configuration for
MethodInvokingTaskletAdapter
.