spring-boot动态添加、删除定时任务

一、注入依赖

<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.1</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-api</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

二、编写执行任务

import org.jboss.logging.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * Created by 赵亚辉 on 2017/12/18.
 */
public class ScheduledJob implements Job {

    private static final Logger logger= Logger.getLogger(ScheduledJob.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("schedule job1 is running");
    }
}
三、任务管理器

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

/**
 * Created by 赵亚辉 on 2017/12/18.
 */
@Component
public class SchedulerAllJob {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /*
     * 此处可以注入数据库操作,查询出所有的任务配置
     */

    /**
     * 该方法用来启动所有的定时任务
     * @throws SchedulerException
     */
    public void scheduleJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        scheduleJob1(scheduler);

    }

    public void stop() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        scheduler.clear();
    }

    /**
     * 配置Job1
     * 此处的任务可以配置可以放到properties或者是放到数据库中
     * @param scheduler
     * @throws SchedulerException
     */
    private void scheduleJob1(Scheduler scheduler) throws SchedulerException{
        /*
         *  此处可以先通过任务名查询数据库,如果数据库中存在该任务,则按照ScheduleRefreshDatabase类中的方法,更新任务的配置以及触发器
         *  如果此时数据库中没有查询到该任务,则按照下面的步骤新建一个任务,并配置初始化的参数,并将配置存到数据库中
         */
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
        // 每5s执行一次
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }
    
}
四、编写测试接口

import com.example.demo.job.SchedulerAllJob;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 赵亚辉 on 2017/12/18.
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    public SchedulerAllJob myScheduler;

    @RequestMapping("/start")
    public String schedule() throws SchedulerException {

        myScheduler.scheduleJobs();

        return "success";
    }

    @RequestMapping("/stop")
    public String stop() throws SchedulerException {

        myScheduler.stop();

        return "success";
    }
}


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页