生成任务定时取消
一、数据库轮询操作
思路:
通过quartiz实现每相隔一定的时间对某个任务进行定时扫描,maven依赖如下:
具体的测试代码如下所示:
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; public class MyJob implements Job { public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("扫描数据库"); } public static void main(String[]args) throws SchedulerException { //创建执行任务 JobDetail jobDetail= JobBuilder.newJob(MyJob.class).withIdentity("Job1","group1").build(); //创建触发器 Trigger trigger=TriggerBuilder.newTrigger().withIdentity("trigger1","group3") .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build(); //创建定时器 Scheduler scheduler=new StdSchedulerFactory().getScheduler(); scheduler.scheduleJob(jobDetail,trigger); //加载任务和触发器 scheduler.start(); } }
没有停止的脚步,只有倒下去的脚步