Skip to content

Instantly share code, notes, and snippets.

@aziz781
Created November 3, 2011 13:27
Show Gist options
  • Save aziz781/1336475 to your computer and use it in GitHub Desktop.
Save aziz781/1336475 to your computer and use it in GitHub Desktop.
Running a Spring Batch Job in The Spring framework Application
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
public class QuartzLauncher extends QuartzJobBean {
protected final Log log = LogFactory.getLog(getClass());
static final String JOB_NAME = "jobName";
private JobLocator jobLocator;
private JobLauncher jobLauncher;
public void setJobLocator(JobLocator jobLocator) {
this.jobLocator = jobLocator;
}
public void setJobLauncher(JobLauncher jobLauncher) {
this.jobLauncher = jobLauncher;
}
@SuppressWarnings("unchecked")
public void executeInternal(JobExecutionContext context) {
Map<String, Object> jobDataMap = context.getMergedJobDataMap();
String jobName = (String) jobDataMap.get(JOB_NAME);
log.info("Quartz trigger firing with Spring Batch jobName=" + jobName);
// prepare job parameter
JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("orderDate", new Date());
JobParameters jobParameters = builder.toJobParameters();
try {
jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
}
catch (JobExecutionException e) {
log.error("Could not execute job.", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment