Java Reference
In-Depth Information
9-3. Running Jobs
Problem
What deployment scenarios does Spring Batch support? How does Spring Batch “launch”? How does
Spring Batch work with a system scheduler such as cron or autosys , or from a web application?
Solution
Spring Batch provides facilities to address execution inside of a web environment. It also provides a
convenience class that can be readily used with cron or autosys to support launching job s.
How It Works
Before you get into creating a solution, it's important to know what options are available for deploying
and running these solutions. All solutions require, at minimum, a job and a JobLauncher . You already
configured these components in the previous recipe. The job is configured in your Spring XML
application context, as you'll see later. The simplest example of launching a Spring Batch solution from
Java code is about five lines of Java code, three if you've already got a handle to the ApplicationContext !
package com.apress.springenterpriserecipes.springbatch.solution1;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws Throwable {
// build a standard ApplicationContext
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext
("customSolution.xml");
classPathXmlApplicationContext.start();
// launch the job
JobLauncher jobLauncher = (JobLauncher)
classPathXmlApplicationContext.getBean("jobLauncher");
Job job = (Job) classPathXmlApplicationContext.getBean(
"nameOfCustomJobHere");
jobLauncher.run(job, new JobParameters());
}
}
This mechanism is the most flexible, and indeed it's sometimes required. If you plan to use the
popular Quartz scheduling framework, you need to write a job that launches a batch job that looks
something like what you see.
 
Search WWH ::




Custom Search