1. 程式人生 > >springboot整合quartz實現任務排程

springboot整合quartz實現任務排程

quartz

概述
  • 特點
    • 強大的排程功能
    • 靈活的應用方式
    • 分散式和叢集能力
  • 用到的設計模式
    • Builder 模式
    • factory模式
    • 元件模式
    • 鏈式寫法
  • 體系結構
    • 排程器
    • 任務
    • 觸發器
  • 架構圖
    • Alt text

      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;
    }
}