springboot整合quartz异常

背景:启动服务器时,启动定时任务,同时数据库记录了定时任务的信息,一切都是那么正常。但是我关闭了IDE,重启系统时异常发生了,报错如下:

org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'event_job_group.task_SaveFlightDataTask1', because one already exists with this identification。

经过分析得到的结论是由于数据库已经存在了task 的定义了,就是说重复了,只要将注入到容器当中的Scheduler 在初始化的时候clear()一下就好了:

scheduler.clear();

 这波操作没问题,但是我就是那么诡异,经此操作后我在系统中调用接口添加的定时任务也一起没了!!!一起被清除掉了!!! 怎么解决呢?

好吧,上面的clear肯定不能再用了。

换种思路来解决:

启动系统时,先把已经存在的任务删除了再创建。注意:根据jobKey来删除。

/**
 * 监听器,启动定时任务
 *
 * @author yangfeng
 * @date 2019-03-12
 */
@Configuration
public class ApplicationStartQuartzJobListener implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger LOG = LoggerFactory.getLogger(ApplicationStartQuartzJobListener.class);

    @Autowired
    private QuartzService quartzService;


    /**
     * 初始启动quartz
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
             
             LOG.info("************************定时任务启动开始******************");
             quartzService.createScheduleJob("SaveDataTask",
                            "0 */10 * * * ?", SaveDataTask.class, null);
          }
    }
}
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 定时任务服务
 *
 * @author yangfeng
 * @date 2018-07-03
 */
@Service
public class QuartzService {

    private static final String JOB_GROUP = "event_job_group";
    private static final String TRIGGER_GROUP = "event_trigger_group";

    @Autowired
    private Scheduler scheduler;

    /**
     * 创建定时任务,通过注入Scheduler对任务进行操作
     *
     * @param jobDetailName
     * @param cronExpression
     * @param jobClass
     * @throws SchedulerException
     */
    public void createScheduleJob(String jobDetailName, String cronExpression, Class<? extends Job> jobClass, String param) throws SchedulerException {
        //先把已经有的删除了
        deleteScheduleJob(jobDetailName, JOB_GROUP);
        JobDetail jobDetail = JobBuilder.newJob(jobClass)
                .withIdentity("task_" + jobDetailName, JOB_GROUP).storeDurably().requestRecovery().build();
        //参数传递
        if (!StringUtils.isEmpty(param)) {
            jobDetail.getJobDataMap().put("task_" + jobDetail.getKey(), param);
        }
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("task_" + jobDetailName, TRIGGER_GROUP).withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
    }

    /**
     * 删除定时任务
     *
     * @param jobDetailName
     * @param jobGroup
     * @return
     * @throws SchedulerException
     */
    public boolean deleteScheduleJob(String jobDetailName, String jobGroup) throws SchedulerException {
        JobKey jobKey = JobKey.jobKey("task_" + jobDetailName, jobGroup);
        return scheduler.deleteJob(jobKey);
    }
}

 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页