Java Reference
In-Depth Information
Listing 5-7. JobExplorer Configuration
<beans:bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
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=" http://www.springframework.org/schema/batch"
xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
<import resource="../launch-context.xml"/>
<bean id="helloWorld"
class="com.apress.springbatch.chapter5.HelloWorld">
<property name="explorer" ref="jobExplorer"/>
</bean>
<batch:step id="helloWorldStep">
<batch:tasklet ref="helloWorld"/>
</batch:step>
<batch:job id="helloWorldJob">
<batch:step id="step1" parent="helloWorldStep"/>
</batch:job>
</beans>
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 run.id 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
tasklet.
 
Search WWH ::




Custom Search