Skip to content

Instantly share code, notes, and snippets.

@aziz781
Created November 3, 2011 13:30
Show Gist options
  • Save aziz781/1336483 to your computer and use it in GitHub Desktop.
Save aziz781/1336483 to your computer and use it in GitHub Desktop.
Java Spring Batch: Running a Spring Batch Job
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class PwsJobLauncher {
protected final Log log = LogFactory.getLog(getClass());
@Autowired
@Qualifier("pwsDailyJob")
private Job job;
@Autowired
private JobLauncher jobLauncher;
JobParameters jobParameters;
public void setJobLauncher(JobLauncher jobLauncher) {
this.jobLauncher = jobLauncher;
}
public JobExecution execute(String orderDate, String test) throws JobExecutionException,ParseException{
if(orderDate==null || "".compareTo(orderDate.trim())==0)
{
throw new JobExecutionException("can not run the job as Date is missing or invalid");
}
JobParametersBuilder jobParametersbuilder = new JobParametersBuilder().
addDate("orderDate", getDate(orderDate)); //dd-MM-yyyy
if(!(test==null || "".compareTo(test.trim())==0))
jobParametersbuilder.addString("test", test);
jobParameters = jobParametersbuilder.toJobParameters();
log.info("Launching Job '"+job.getName()+"' with Parameters: "+jobParameters);
// Launch the job
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
return jobExecution;
}
private Date getDate(String dateStr) throws ParseException
{
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date today = df.parse(dateStr);
return today;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment