pom.xml
Code:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<!-- quartz 2.x not compatible with Spring 3.0.x -->
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-oracle</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
code for QuartzJobLauncher.java
Code:
public class QuartzJobLauncher extends QuartzJobBean implements StatefulJob{
/** Map parameter for getting the job name from job invocation */
private static final String JOB_NAME = "jobName";
/** Map parameter for getting the job launch date from job invocation */
private static final String JOB_LAUNCH_DATE = "job.launch.date";
/**
* Execute the Quartz Job bean which is passed with a key name 'jobName' in the JobDataMap.
*
* @param context execution context
*/
@SuppressWarnings("unchecked")
protected void executeInternal(final JobExecutionContext context) {
final Map<String,Object> jobDataMap = context.getMergedJobDataMap();
// add the current date to job parameters in order to run the same job
// with same parameters multiple times
if (jobDataMap.get(JOB_LAUNCH_DATE) == null) {
final Calendar calender = Calendar.getInstance();
final Date currentDate = calender.getTime();
jobDataMap.put(JOB_LAUNCH_DATE, currentDate);
}
final String jobName = (String) jobDataMap.get(JOB_NAME);
if (jobName == null) {
throw new IllegalStateException(
"Job Name can't be null, "
+"must be passed in the JobDataMap while executing the batch");
}
final JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
try {
// added for removing the serialization issue
SchedulerContext schedulerContext = context.getScheduler().getContext();
final ApplicationContext applicationContext = (ApplicationContext) schedulerContext
.get("applicationContext");
final JobLauncher jobLauncher = applicationContext.getBean(
"jobLauncher", JobLauncher.class);
final JobLocator jobLocator = applicationContext.getBean("jobRegistry",
JobLocator.class);
final Job jobToRun = jobLocator.getJob(jobName);
jobLauncher.run(jobToRun, jobParameters);
} catch (SchedulerException e) {
throw new IllegalStateException(jobName + " has exception");
}
}
/**
* Get the job parameters passed from configured job map from spring context xml.
*
* @param jobDataMap the Map
* @return JobParameters from job data map
*/
private JobParameters getJobParametersFromJobMap(
final Map<String, Object> jobDataMap) {
final JobParametersBuilder builder = new JobParametersBuilder();
for (Map.Entry<String,Object> entry : jobDataMap.entrySet()) {
final String key = (String) entry.getKey();
final Object value = entry.getValue();
if (((value instanceof String)) && (!key.equals(JOB_NAME))) {
builder.addString(key, (String) value);
} else if (((value instanceof Float))
|| ((value instanceof Double))) {
builder.addDouble(key,
Double.valueOf(((Number) value).doubleValue()));
} else if (((value instanceof Integer))
|| ((value instanceof Long))) {
builder.addLong(key, Long.valueOf(((Number) value).longValue()));
} else if ((value instanceof Date)) {
builder.addDate(key, (Date) value);
}/* else {
//ignore
log.debug("JobDataMap contains values which are not job parameters (ignoring).");
}*/
}
return builder.toJobParameters();
}
}
java code for TestTasklet.java
Code:
public class TestTasklet implements Tasklet {
@Override
public RepeatStatus execute(final StepContribution contribution,
final ChunkContext chunkContext) {
Random random = new Random();
int randomNo = random.nextInt(1000);
System.out.println("Tasklet TestTasklet###"+randomNo+" for spring-test started @ "+new Date());
//some code that executes for more than schedule time
System.out.println("Tasklet TestTasklet###"+randomNo+" for spring-test ended @ "+new Date());
return RepeatStatus.FINISHED;
}
}
java code for TestTaskletOne.java (same as TestTasklet.java)