Java Reference
In-Depth Information
Note
Passing in a configuration that has a job that has already been referenced leads to a
DuplicateJobException
being thrown.
Instead of using the
Batch
class you wrote in the previous chapter, you use
JobRegistryBackgroundJobRunner
to bootstrap your job. Because
JobRegistryBackgroundJobRunner
is
included in the
spring-batch-core
jar file (the main Spring Batch dependency), the only change you
need to make to execute the jar file with
JobRegistryBackgroundJobRunner
is to change the POM file to
reference
JobRegistryBackgroundJobRunner
as the main class. Listing 6-5 highlights the change.
Listing 6-5.
Changing the Main Class in
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<index>false</index>
<manifest>
<mainClass>
org.springframework.batch.core.launch.support.JobRegistryBackgroundJobRunner
</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestFile>
${project.build.outputDirectory}/META-INF/MANIFEST.MF
</manifestFile>
</archive>
</configuration>
</plugin>
That small change in
pom.xml
is all that you need to make to be able to execute the jar file using
JobRegistryBackgroundJobRunner
. When the jar file is rebuilt, you can execute it with the command in
Listing 6-6.
Listing 6-6.
Executing
JobRegistryBackgroundJobRunner
java -jar helloWorld.jar launch-context.xml jobs/helloWorld.xml
Notice that you don't specify the name of the job you want to be run on the command line as you
have in the past. The reason is that you're only bootstrapping Spring and Spring Batch with this
command and not initiating the execution of a job. When you execute the jar, you see your normal
Spring bootstrap output, finishing with what is shown in Listing 6-7 Then the application waits. And
waits. It continues to run until either it's killed or the
JobRegistryBackgroundJobRunner.stop()
method is
called programmatically. With the process now running, you can use JConsole as you did in Chapter 5 to
execute the job.