二、方法org.springframework.boot spring-boot-starter-quartz
-
启动任务
public void start(Class> jobClass, String name, String group, String cron) { try { if (scheduler.checkExists(new JobKey(name, group))) { // 判断定时器是否存在,存在则删除 this.delete(name, group); } // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例,JobDetail 是具体Job实例 JobDetail jobDetail = JobBuilder.newJob((Class extends Job>) jobClass).withIdentity(name, group).build(); // 基于表达式构建触发器 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron); // CronTrigger表达式触发器 继承于Trigger,TriggerBuilder 用于构建触发器实例 CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(name, group) .withSchedule(cronScheduleBuilder).build(); scheduler.scheduleJob(jobDetail, cronTrigger); scheduler.start(); } catch (Exception schedulerException) { throw new RuntimeException(schedulerExceptio n.getMessage()); } }
-
删除任务
public void delete(String name, String group) { try { JobKey jobKey = new JobKey(name, group); JobDetail jobDetail = scheduler.getJobDetail(jobKey); if (jobDetail == null) { return; } scheduler.deleteJob(jobKey); } catch (SchedulerException schedulerException) { schedulerException.printStackTr ace(); } }
-
判断是否存在
public Boolean checkExists(String name, String group) { try { return scheduler.checkExists(new JobKey(name, group)); } catch (SchedulerException e) { return false; } }
cron在线表达式
四、TODO…欢迎分享,转载请注明来源:内存溢出
评论列表(0条)