Hi,
I'm beginner with spring quartz and i have a problem for my project.
I'would whan my job is failed or sleep, the scheduler relance it and the job can retake
where it has stopped.
This is a differents steps how i was did :
In the first i have m y class scheduler:
Code:
@DisallowConcurrentExecution
public class JobLauncherDetails extends QuartzJobBean /*implements JobExecutionListener*/ {
private static final String JOB_DATA_MAP_MAX_RETRY = "maxRetry";
//private static final String JOB_DATA_MAP_NB_RETRIES = "nbRetries";
private final static String JOB_LOCATOR_CONTEXT_KEY = "jobLocator";
private final static String JOB_LAUNCHER_CONTEXT_KEY = "jobLauncher";
private static final String JOB_DATA_MAP_START_DATE = "startDate";
private static final String JOB_PARAM_LISTENER_DELAY_KEY = "listenerDelay";
private static final long JOB_PARAM_DEFAULT_LISTENER_DELAY = 1000;
private static Logger log = LoggerFactory.getLogger(JobLauncherDetails.class);
/**
* Special key in job data map for the name of a job to run.
*/
private static final String JOB_NAME = "jobName";
private JobLocator jobLocator;
private JobLauncher jobLauncher;
private String jobName;
private JobListener MyjobListener;
/**
* Method called by Quartz trigger. The given {@link JobExecutionContext}
* gives info on the Job to run.
*/
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
jobLocator = (JobLocator) context.getScheduler().getContext().get(JOB_LOCATOR_CONTEXT_KEY);
jobLauncher = (JobLauncher) context.getScheduler().getContext().get(JOB_LAUNCHER_CONTEXT_KEY);
MyjobListener=(JobListener)context.getScheduler().getContext().get(MyjobListener);
} catch (SchedulerException se) {
log.error("Unable to get jobLocator and jobLauncher from scheduler context.", se);
}
if (jobLocator == null || jobLauncher == null) {
log.error("Unable to run a job without valids jobLocator and jobLauncher.");
} else {
JobDetail jobDetail = context.getJobDetail();
Map<String, Object> jobDataMap = context.getMergedJobDataMap();
if (jobDataMap == null || jobDataMap.size() == 0) {
log.error("Unable to run a job without a valid jobDataMap (no job name provided...).");
} else {
jobName = (String) jobDataMap.get(JOB_NAME);
if (jobName == null || jobName.isEmpty()) {
log.error("Unable to run a job: no job name provided...");
} else {
if(context.getRefireCount()==0) {
// Add a date to the jobDataMap so that the job is unique.
// It is useful to distinguish 2 instances of the same trigger.
// Otherwise, a JobExecutionAlreadyRunningException will be launched
// (no need to modify this parameter for a refired job)
jobDataMap.put(JOB_DATA_MAP_START_DATE, new Date());
}
JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
Scheduler sched= context.getScheduler();
try {
sched.getListenerManager().addJobListener(new NcaJobListener());
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (log.isInfoEnabled())
log.info("\n**********************************************************************\n"
+ "* Quartz trigger - start job: {}. Key: {}\n"
+ "* Firing unique ID: {}\n"
+ "* Refire count: {}\n"
+ "* Job parameters: {}\n"
+ "* isConcurrentExectionDisallowed: {}\n"
+ "**********************************************************************\n",
new Object[] { jobName, jobDetail==null?"":jobDetail.getKey(), context.getFireInstanceId(),
context.getRefireCount(), jobParameters==null?"":jobParameters.toString(),
context.getJobDetail().isConcurrentExectionDisallowed() });
JobExecution jobExec = null;
BatchStatus jobStatus = BatchStatus.UNKNOWN;
try {
jobExec = jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
log.info("Job Id: {}", jobExec.getId());
}
} catch (Exception ex) {
if(!(ex instanceof JobExecutionException)) {
log.error("Error while running Job [{}]. Rescheduling if possible.\nError: {}\n******************************", jobName, ex.toString());
// TODO: retirer ce log...
log.error("Error full stack:", ex);
} else {
throw (JobExecutionException) ex;
}
}
if (log.isInfoEnabled())
log.info("\n**********************************************************************\n"
+ "* Quartz trigger - end job: {}\n"
+ "* Firing unique ID: {}\n"
+ "* Refire count: {}\n"
+ "* Job parameters: {}\n"
+ "* Start date: {}\n"
+ "* End date: {}\n"
+ "* Status: {}\n"
+ "**********************************************************************\n",
new Object[] { jobName, context.getFireInstanceId(), context.getRefireCount(),
jobParameters==null?"":jobParameters.toString(),
jobExec==null?"":jobExec.getStartTime(),
jobExec==null?"":jobExec.getEndTime(),
jobStatus });
}
}
}
}
/**
* Copy parameters that are of the correct type over to
* {@link JobParameters}, ignoring jobName.
*
* @return a {@link JobParameters} instance
*/
private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {
JobParametersBuilder builder = new JobParametersBuilder();
for (Entry<String, Object> entry : jobDataMap.entrySet()) {
String key = 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, ((Number) value).doubleValue());
} else if (value instanceof Integer || value instanceof Long) {
builder.addLong(key, ((Number) value).longValue());
} else if (value instanceof Date) {
builder.addDate(key, (Date) value);
} else {
log.debug("JobDataMap contains values which are not job parameters (ignoring).");
}
}
return builder.toJobParameters();
}
/*@Override
public void afterJob(JobExecution jobExecution) {
// TODO Auto-generated method stub
log.info("*************************** Job ended with status: {} ********************", jobExecution.getExitStatus());
if (ExitStatus.FAILED.equals(jobExecution.getExitStatus())) {
log.error("******************************\nJob [{}] failed. Reschedule if possible.\n******************************", jobName);
// rescheduleJob(jobExecution);
}
}
@Override
public void beforeJob(JobExecution jobExecution) {
log.info("************** Before running job {} **************", jobName);
}*/
}