SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)

一、新建web工程,引入quatrz的包等

SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)_第1张图片

pom.xml如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.cobra
    quartzlearn
    0.0.1-SNAPSHOT
    quartzlearn
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-quartz
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、coding:

目录结构如下:

SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)_第2张图片

1、在job包下,新建MyJob类implements实现Job接口,在execute方法里面写上具体的业务:

package com.cobra.quartzlearn.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/13 23:17
 */
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //具体业务
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("现在的时间是:"+sf.format(date));
        System.out.println("Hello Quartz");
    }
}

2、在controller包下新建QuartzController类:

package com.cobra.quartzlearn.controller;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/14 0:06
 */
@RestController
@RequestMapping("/schedule")
public class QuartzController {

    /**
     * 利用SimpleTrigger设定运行机制
     * @return
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    @GetMapping("/bySimple")
    public String executeOnTime() throws ClassNotFoundException, SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("myFirstJob").build();
        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("myTrigger").startNow().withSchedule(
                SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, simpleTrigger);
        return "执行bySimple";
    }

    /**
     * 利用CronTrigger设定运行机制
     * @return
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    @GetMapping("/byCron")
    public String execute() throws SchedulerException, ClassNotFoundException {
        JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("mySecondJob").build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")).build();
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, cronTrigger);
        return "执行byCron";
    }

    /**
     * 通过类路径获取类
     * @return
     * @throws ClassNotFoundException
     */
    private Class getMyClass() throws ClassNotFoundException {
        Class myClass = Class.forName("com.cobra.quartzlearn.job.MyJob");
        return myClass;
    }

}

三、启动项目测试:

1、访问localhost:8080/schedule/bySimple,控制台输出:

SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)_第3张图片

2、访问localhost:8080/schedule/byCron,控制台输出:

SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)_第4张图片

四、可控制停止开启任务的demo

1、新建类ScheduleController 

package com.cobra.quartzlearn.controller;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/14 0:06
 */
@RestController
@RequestMapping("/quartz")
public class ScheduleController {

    /**
     * 开始执行任务
     * @return
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    @GetMapping("/start")
    public String start() throws ClassNotFoundException, SchedulerException {
        Scheduler scheduler = createScheduler();
        scheduler.start();
        return "执行任务!!!!!";
    }

    /**
     * 停止执行任务
     * @return
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    @GetMapping("/stop")
    public String stop() throws SchedulerException, ClassNotFoundException {
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        JobKey jobKey = new JobKey("myStoppableJob");
        scheduler.pauseJob(jobKey);
        scheduler.deleteJob(jobKey);
        return "停止任务!!!!!";
    }

    /**
     * 新建任务
     * @return
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    public Scheduler createScheduler() throws SchedulerException, ClassNotFoundException {
        JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("myStoppableJob").build();
        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("myStoppableTrigger").startNow().withSchedule(
                SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.scheduleJob(jobDetail, simpleTrigger);
        return scheduler;
    }

    /**
     * 通过类路径获取类
     * @return
     * @throws ClassNotFoundException
     */
    private Class getMyClass() throws ClassNotFoundException {
        Class myClass = Class.forName("com.cobra.quartzlearn.job.MyJob");
        return myClass;
    }

}

2、重启项目,测试访问localhost:8080/quartz/start,控制台输出日志:

SpringBoot2.X之旅,Quartz的简单使用,实现定时任务的一种实现方式,可以控制启动和停止(Web Project)_第5张图片

访问localhost:8080/quartz/stop,控制台停止输出日志

 

你可能感兴趣的:(springboot,springboot2.x,quartz,quartz任务管理,springboot定时任务)