我已经编写了以下代码来使用石英调度程序安排作业,这些作业恰好在几天(3或4天)内正确触发,并突然停止在第二天完全触发。请告诉我代码中的任何错误。
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class JobSchedulerServiceImpl implements JobSchedulerService {
private static LoggerUtil logger = new LoggerUtil(JobSchedulerServiceImpl.class);
@Autowired
StdSchedulerFactory facSchedulerFactory;
@Override
public boolean createJob(TaskSchedulerInfo taskSchedulerInfo) throws SchedulerException {
boolean flag = false;
switch(taskSchedulerInfo.getTaskId()){
case "DATA_EXPORT" :
return scheduleJob(new ShopperDataJobs().getClass(), taskSchedulerInfo);
case "NEW_DWELL_UPDATE" :
return scheduleJob(new OptimizedDataToJob().getClass(), taskSchedulerInfo);
case "DASHBOARD" :
return scheduleJobTimezone(new DashboardCacheJob().getClass(), taskSchedulerInfo);
case "RESPONSE_TIME" :
return scheduleJob(new ResponseTimeReportJob().getClass(), taskSchedulerInfo);
case "CONVERSION_RATE" :
return scheduleJob(new ConversionRateReportJob().getClass(), taskSchedulerInfo);
case "STORE_TRAFFIC" :
return scheduleJob(new StoreTrafficReportJob().getClass(), taskSchedulerInfo);
case "DWELL_TIME" :
return scheduleJob(new DwellTimeReportJob().getClass(), taskSchedulerInfo);
case "STORE_SUMMARY_VIEW" :
return scheduleJob(new StoreSummaryDataExportJob().getClass(), taskSchedulerInfo);
case "DASHBOARD_CACHE" :
return scheduleJob(new DashboardCacheJob().getClass(), taskSchedulerInfo);
case "LOCATION_SUMMARY_VIEW" :
return scheduleJob(new LocationSummaryJob().getClass(), taskSchedulerInfo);
case "DATA_EXPORT_SHIFT_TIMING" :
return scheduleJob(new ShopperShiftTimingDataJob().getClass(), taskSchedulerInfo);
}
return flag;
}
public boolean scheduleJob(Class<? extends Job> obj, TaskSchedulerInfo taskSchedulerInfo) throws SchedulerException{
boolean flag = false;
Scheduler scheduler = facSchedulerFactory.getScheduler();
logger.info("Scheduling Job for "+taskSchedulerInfo.getTaskId());
logger.info("Cron Expression " +taskSchedulerInfo.getCronExpression());
if(!checkForExistingJobForTask(taskSchedulerInfo)){
JobDetail job = getJobDetail(obj, taskSchedulerInfo);
Trigger trigger = getTrigger(job, taskSchedulerInfo.getCronExpression(), taskSchedulerInfo);
scheduler.start();
if(trigger != null){
scheduler.scheduleJob(job, trigger);
flag = true;
}
}
logger.info("Flag for task is "+flag);
return flag;
}
public boolean scheduleJobTimezone(Class<? extends Job> obj, TaskSchedulerInfo taskSchedulerInfo) throws SchedulerException{
boolean flag = false;
Scheduler scheduler = facSchedulerFactory.getScheduler();
if(!checkForExistingJobForTask(taskSchedulerInfo)){
JobDetail job = getJobDetailTimeZone(obj, taskSchedulerInfo);
Trigger trigger = getTrigger(job, taskSchedulerInfo.getCronExpression(), taskSchedulerInfo);
scheduler.start();
if(trigger != null){
scheduler.scheduleJob(job, trigger);
flag = true;
}
}
return flag;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean updateJob(TaskSchedulerInfo taskSchedulerInfo) throws SchedulerException {
boolean flag = false;
Scheduler scheduler = facSchedulerFactory.getScheduler();
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(JobKey.jobKey(taskSchedulerInfo.getScheduleId()));
if(triggers != null && triggers.size()>0){
Trigger t= triggers.get(0);
TriggerBuilder tb = t.getTriggerBuilder();
Trigger newTrigger = null;
if(taskSchedulerInfo.getTimezone()!=null && !taskSchedulerInfo.getTimezone().isEmpty()){
if ((Constants.TaskFrequency.Daily.name()).equalsIgnoreCase(taskSchedulerInfo.getFrequency())) {
newTrigger = tb.startAt(Date.from(taskSchedulerInfo.getStartDateTime().atZone(ZoneId.of(taskSchedulerInfo.getTimezone())).toInstant()))
.withIdentity(taskSchedulerInfo.getScheduleId())
.withSchedule(CronScheduleBuilder.cronSchedule(taskSchedulerInfo.getCronExpression()).inTimeZone(TimeZone.getTimeZone(taskSchedulerInfo.getTimezone()))).build();
} else {
newTrigger = tb.withIdentity(taskSchedulerInfo.getScheduleId())
.withSchedule(CronScheduleBuilder.cronSchedule(taskSchedulerInfo.getCronExpression()).inTimeZone(TimeZone.getTimeZone(taskSchedulerInfo.getTimezone()))).build();
}
}
if(newTrigger!=null){
logger.info("Updated Job for "+taskSchedulerInfo.getTaskId());
logger.info("Cron Expression " +taskSchedulerInfo.getCronExpression());
scheduler.rescheduleJob(t.getKey(), newTrigger);
flag = true;
}
}
return flag;
}
public JobDetail getJobDetail(Class<? extends Job> obj, TaskSchedulerInfo taskSchedulerInfo) {
return newJob(obj).withIdentity(JobKey.jobKey(taskSchedulerInfo.getScheduleId()))
.usingJobData("tenantId", taskSchedulerInfo.getTenantId())
.usingJobData("storeId", taskSchedulerInfo.getBaseStore().getStoreId())
.usingJobData("timeZone", taskSchedulerInfo.getTimezone()).build();
}
public JobDetail getJobDetailTimeZone(Class<? extends Job> obj, TaskSchedulerInfo taskSchedulerInfo) {
return newJob(obj).withIdentity(JobKey.jobKey(taskSchedulerInfo.getScheduleId()))
.usingJobData("tenantId", taskSchedulerInfo.getTenantId())
.usingJobData("storeId", taskSchedulerInfo.getBaseStore().getStoreId())
.usingJobData("timezone", taskSchedulerInfo.getTimezone())
.build();
}
public Trigger getTrigger(JobDetail job, String cron, TaskSchedulerInfo taskSchedulerInfo) {
Trigger trigger = null;
if(taskSchedulerInfo.getTimezone()!=null && !taskSchedulerInfo.getTimezone().isEmpty()){
if ((Constants.TaskFrequency.Daily.name()).equalsIgnoreCase(taskSchedulerInfo.getFrequency())) {
trigger = newTrigger().forJob(job)
.startAt(Date.from(taskSchedulerInfo.getStartDateTime().atZone(ZoneId.of(taskSchedulerInfo.getTimezone())).toInstant()))
.withIdentity(taskSchedulerInfo.getScheduleId())
.withSchedule(CronScheduleBuilder.cronSchedule(cron).inTimeZone(TimeZone.getTimeZone(taskSchedulerInfo.getTimezone())).withMisfireHandlingInstructionDoNothing())
.build();
} else {
trigger = newTrigger().forJob(job).withIdentity(taskSchedulerInfo.getScheduleId())
.withSchedule(CronScheduleBuilder.cronSchedule(cron).inTimeZone(TimeZone.getTimeZone(taskSchedulerInfo.getTimezone())).withMisfireHandlingInstructionDoNothing()).build();
}
}
return trigger;
}
public boolean checkForExistingJobForTask(TaskSchedulerInfo taskSchedulerInfo) throws SchedulerException{
Scheduler scheduler = facSchedulerFactory.getScheduler();
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(JobKey.jobKey(taskSchedulerInfo.getScheduleId()));
boolean flag = false;
if(triggers != null && triggers.size()>0){
flag = true;
}
return flag;
}
<!--deleting jobs -->
@Override
public boolean deleteJobs(List<TaskSchedulerInfo> taskSchedulerInfos)
throws SchedulerException {
<!-- getting scheduler factory bean object-->
Scheduler scheduler = facSchedulerFactory.getScheduler();
boolean status =false;
List<JobKey> jobkeys = new ArrayList<JobKey>();
for(TaskSchedulerInfo taskSchedulerInfo : taskSchedulerInfos){
if(checkForExistingJobForTask(taskSchedulerInfo)){
jobkeys.add(JobKey.jobKey(taskSchedulerInfo.getScheduleId()));
}
}
if(jobkeys != null && jobkeys.size()>0){
scheduler.deleteJobs(jobkeys);
status=true;
}
return status;
}
}