springboot集成quartz实现任务调度

Posted 腾飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot集成quartz实现任务调度相关的知识,希望对你有一定的参考价值。

quartz

概述
  • 特点
    • 强大的调度功能
    • 灵活的应用方式
    • 分布式和集群能力
  • 用到的设计模式
    • Builder 模式
    • factory模式
    • 组件模式
    • 链式写法
  • 体系结构
    • 调度器
    • 任务
    • 触发器
  • 架构图
    • 技术分享图片

      spring boot quartz
  • pom配置
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
  • 编写job类
package com.ltf.shop.quartzdemo;

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

public class HelloJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("hello word quartz");
    }
}
  • 编写quartz定义类
package com.ltf.shop.quartzconfig;

import com.ltf.shop.quartzdemo.HelloJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {
    /**
     * 创建Job对象
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean()
    {
        JobDetailFactoryBean factory=new JobDetailFactoryBean();
        factory.setJobClass(HelloJob.class);
        return  factory;
    }

    /**
     * 创建Job对象
     */
//    @Bean
//    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean)
//    {
//        SimpleTriggerFactoryBean simpleTriggerFactoryBean=new SimpleTriggerFactoryBean();
//        //关联JobDetail对象
//        simpleTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
//        //设置执行的毫秒数
//        simpleTriggerFactoryBean.setRepeatInterval(3000);
//        simpleTriggerFactoryBean.setRepeatCount(15);
//        return simpleTriggerFactoryBean;
//    }

//    /**
//     * 创建scheduler对象
//     */
//    @Bean
//    public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean)
//    {
//        SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();
//        schedulerFactoryBean.setTriggers(simpleTriggerFactoryBean.getObject());
//        return schedulerFactoryBean;
//    }


    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean)
    {
        CronTriggerFactoryBean cronTriggerFactoryBean=new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        cronTriggerFactoryBean.setCronExpression("0/2 * * * * ?");
        return cronTriggerFactoryBean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean)
    {
        SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        return schedulerFactoryBean;
    }
}

以上是关于springboot集成quartz实现任务调度的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot系列:Spring Boot集成定时任务Quartz

SpringBoot 1.5.x 集成 Quartz 任务调度框架

SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?

SpringBoot系列:Spring Boot集成定时任务Quartz

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置

SpringBoot整合Quartz实现动态的创建或删除定时任务并将定时调度任务持久化到MySQL以及Quartz集群配置