SpringBoot整合Quartz定时任务

SpringBoot整合Quartz定时任务

创建工程
SpringBoot整合Quartz定时任务_第1张图片
SpringBoot整合Quartz定时任务_第2张图片
SpringBoot整合Quartz定时任务_第3张图片

创建任务类

package com.quartzjob.job.sysJob;

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

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

/**
 * 具体任务
 */
public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
        String dateString = simpleDateFormat.format(date);
        System.out.println(dateString);
    }
}

配置触发器和任务调度器

package com.quartzjob.job.config;

import com.quartzjob.job.sysJob.HelloJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * 配置触发器和任务调度器
 */
@Configuration
public class QuartzConfig {
    /**
     * 创建job对象
     * @return
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factoryBean =  new JobDetailFactoryBean();
        //具体任务类
        factoryBean.setJobClass(HelloJob.class);
        return factoryBean;
    }

    /**
     * 创建tigger对象
     * @param jobDetailFactoryBean
     * @return
     */
    @Bean
    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        //关联jobDetailFactoryBean
        factoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        //执行周期
        factoryBean.setRepeatInterval(2000);
        //重复次数
        factoryBean.setRepeatCount(5);
        return factoryBean;
    }

    /**
     * 创建Scheduler
     * @param simpleTriggerFactoryBean
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        //关联tigger
        factoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
        return factoryBean;
    }
}


开启定时任务

package com.quartzjob.job;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class JobApplication {

    public static void main(String[] args) {
        SpringApplication.run(JobApplication.class, args);
    }

}

手动控制定时器开启或关闭

package com.quartzjob.job.controller;

import com.quartzjob.job.sysJob.HelloJob;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时器controller
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    /**
     * 手动开启定时器
     */
    @GetMapping("/start")
    public void start() {
        try {
            SchedulerFactory sf = new StdSchedulerFactory();
            Scheduler scheduler = sf.getScheduler();
            if (scheduler.getJobDetail(new JobKey("HelloJob")) == null) {
                JobDetail myJob = new JobDetailImpl("HelloJob", Scheduler.DEFAULT_GROUP, HelloJob.class);
                Trigger trigger = new CronTriggerImpl("HelloTrigger", Scheduler.DEFAULT_GROUP, "0/2 * * * * ?");
                scheduler.scheduleJob(myJob, trigger);
                scheduler.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 手动关闭定时器
     */
    @GetMapping("/close")
    public void close() {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            if (scheduler.getJobDetail(new JobKey("HelloJob")) != null) {
                scheduler.deleteJob(new JobKey("HelloJob"));
                System.out.println("定时器关闭");
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,java,quartz)