生成任务定时取消


 

一、数据库轮询操作

思路:

通过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();

    }
}









 

posted @ 2021-09-06 18:47  夏的世界的伤  阅读(88)  评论(0编辑  收藏  举报