Java Reference
In-Depth Information
Listing 5-7. JobExplorer Configuration
<beans:bean id="jobExplorer"
p:dataSource-ref="dataSource" />
The JobExplorer is configured just like any other Spring bean with a single dependency—a
datasource—so it can be injected into any of your other elements. Note that unlike most of the other
Spring Batch components you've configured that have a dependency of the JobRepository, this one
depends on a datasource. The reason is that, as mentioned earlier, the JobExplorer doesn't obtain its
information from the JobRepository. Instead, it goes directly to the database for its information.
To see how the JobExplorer works, you can inject it into the HelloWorld tasklet from the previous
examples. From there, you can see what you can use the JobExplorer for. In Listing 5-8, you configure
the HelloWorld tasklet with the JobExplorer injected.
Listing 5-8. Configuration of the HelloWorld tasklet and JobExplorer
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:batch=""
<import resource="../launch-context.xml"/>
<bean id="helloWorld"
<property name="explorer" ref="jobExplorer"/>
<batch:step id="helloWorldStep">
<batch:tasklet ref="helloWorld"/>
<batch:job id="helloWorldJob">
<batch:step id="step1" parent="helloWorldStep"/>
With the JobExplorer configured, there are a number of things you can do with it. Within the Spring
Batch framework, you can use the JobExplorer in the RunIdIncrementer you looked at in Chapter 4 to
look up the previous parameter value. Another place it's used is in the Spring Batch Admin web
application to determine whether a job is currently running before launching a new instance. In the
example, you use it to determine whether this is the first time you've run this JobInstance. If it is, you
print the message “Hello, Michael!” where Michael is a value passed in. If it isn't the first time you've run
the job, you update the message to “Welcome back Michael!” Listing 5-9 has the updated code for this
Search WWH ::

Custom Search