Incomplete - task 89: Resurrect Quartz sample
Add some logging and attempt to create job parameters from the quartz job detail map
This commit is contained in:
@@ -15,10 +15,16 @@
|
||||
*/
|
||||
package org.springframework.batch.sample.quartz;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.configuration.JobLocator;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
@@ -28,16 +34,21 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class JobLauncherDetails extends QuartzJobBean {
|
||||
|
||||
|
||||
/**
|
||||
* Special key in job data map for the name of a job to run.
|
||||
*/
|
||||
private static final String JOB_NAME = "jobName";
|
||||
|
||||
private static Log log = LogFactory.getLog(JobLauncherDetails.class);
|
||||
|
||||
private JobLocator jobLocator;
|
||||
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobLocator}.
|
||||
* @param jobLocator the {@link JobLocator} to set
|
||||
@@ -45,7 +56,7 @@ public class JobLauncherDetails extends QuartzJobBean {
|
||||
public void setJobLocator(JobLocator jobLocator) {
|
||||
this.jobLocator = jobLocator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobLauncher}.
|
||||
* @param jobLauncher the {@link JobLauncher} to set
|
||||
@@ -55,8 +66,10 @@ public class JobLauncherDetails extends QuartzJobBean {
|
||||
}
|
||||
|
||||
protected void executeInternal(JobExecutionContext context) {
|
||||
String jobName = (String) context.getJobDetail().getJobDataMap().get("jobName");
|
||||
JobParameters jobParameters = new JobParameters();
|
||||
Map jobDataMap = context.getMergedJobDataMap();
|
||||
String jobName = (String) jobDataMap.get(JOB_NAME);
|
||||
log.info("Quartz trigger firing with Spring Batch jobName="+jobName);
|
||||
JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
|
||||
try {
|
||||
jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
|
||||
}
|
||||
@@ -71,4 +84,40 @@ public class JobLauncherDetails extends QuartzJobBean {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy parameters that are of the correct type over to
|
||||
* {@link JobParameters}, ignoring jobName.
|
||||
*
|
||||
* @return a {@link JobParameters} instance
|
||||
*/
|
||||
private JobParameters getJobParametersFromJobMap(Map jobDataMap) {
|
||||
|
||||
JobParametersBuilder builder = new JobParametersBuilder();
|
||||
JobParameters jobParameters = builder.toJobParameters();
|
||||
|
||||
for (Iterator iterator = jobDataMap.entrySet().iterator(); iterator.hasNext();) {
|
||||
Entry entry = (Entry) iterator.next();
|
||||
String key = (String) entry.getKey();
|
||||
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) value);
|
||||
}
|
||||
else if (value instanceof Integer || value instanceof Long) {
|
||||
builder.addLong(key, (Long) value);
|
||||
}
|
||||
else if (value instanceof Date) {
|
||||
builder.addDate(key, (Date) value);
|
||||
}
|
||||
else {
|
||||
log.debug("JobDataMap contains values which are not job parameters (ignoring).");
|
||||
}
|
||||
}
|
||||
|
||||
return jobParameters;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user