spring boot quartz example demo

quartz N年前写过,当时用的是spring 3.x. 时至今日,各种技术也已更新换代,其中spring 推出了其spring boot框架,将各种框架工具优雅地整合到spring boot 又是一件需要学习的事情.


通过好几个小时的google查询相关资料,发现了一种比较优雅的方法将quartz 0配置整合到spring boot 中.

首先添加依赖


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


创建一个用于执行任务的类,加上这三个注解

package com.himly.quartz.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 *
 */
@Configuration
@Component
@EnableScheduling
public class Task {

    private void sayHello() {

        System.out.println("hello hello22222222");
    }

    private void sayHello1() {

        System.out.println("hello hello,111111111111");
    }
}

然后创建一个配置类,首先是JobDetails,比如下面代码中jobDetial.setTargetObject指定了执行任务的类

jobDetail.setTargetMethod指定了执行任务的具体方法,将该方法的返回值作为bean注册.

 
  
@Bean(name = "test")
    public MethodInvokingJobDetailFactoryBean testDetail(Task task) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        //并发执行支持,详情请google
        jobDetail.setConcurrent(true);

        //任务基础信息
        jobDetail.setName("test");
        jobDetail.setGroup("test");

        jobDetail.setTargetObject(task);
        jobDetail.setTargetMethod("sayHello");

        return jobDetail;
    }
     


完了以后再配置Trigger,执行时间等信息,比如下面这段代码

JoDetail test是自动注入进来的,spring注入显示按name再按类型,其中参数名会作为bean的名称去spring的容器中查找.

jobDetail注入进来以后将jobDetail放到Trigger中,并设置corn时间,最后返回值同样作为bean注册到spring容器中.

@Bean(name = "testJobTrigger")
    public CronTriggerFactoryBean testJobTrigger(JobDetail test) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(test);
        trigger.setCronExpression("2/3 * * * * ? *");
        trigger.setName("test");

        return trigger;
    }


最后将Trigger注册到调度中心,如下面这段代码

其中Trigger也是又Spring容器自动注入进来的,将所有类型为Trigger的bean都注入进来.

然后注册到调度中心即可,返回值注册为bean到spring容器中.

最后什么都不用管,直接启动spring boot 程序即可.

/**
     * attention:
     * Details:定义quartz调度工厂
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger... triggers) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动,应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器
        bean.setTriggers(triggers);
        return bean;
    }


配置类完整代码,下面会贴github源码链接

package com.himly.quartz.config;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;


/**
 * create_at:MaZheng
 * create_by:${date} ${time}
 */
@Configuration
public class QuartzConfigration {


    @Bean(name = "test")
    public MethodInvokingJobDetailFactoryBean testDetail(Task task) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        //并发执行支持,详情请google
        jobDetail.setConcurrent(true);

        //任务基础信息
        jobDetail.setName("test");
        jobDetail.setGroup("test");

        jobDetail.setTargetObject(task);
        jobDetail.setTargetMethod("sayHello");

        return jobDetail;
    }


    @Bean(name = "test1")
    public MethodInvokingJobDetailFactoryBean testDetail1(Task task) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();

        //并发执行支持,详情请google
        jobDetail.setConcurrent(true);

        //任务基础信息
        jobDetail.setName("test1");
        jobDetail.setGroup("test1");

        jobDetail.setTargetObject(task);
        jobDetail.setTargetMethod("sayHello1");

        return jobDetail;
    }


    @Bean(name = "testJobTrigger")
    public CronTriggerFactoryBean testJobTrigger(JobDetail test) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(test);
        trigger.setCronExpression("2/3 * * * * ? *");
        trigger.setName("test");

        return trigger;
    }

    @Bean(name = "testJobTrigger1")
    public CronTriggerFactoryBean testJobTrigger1(JobDetail test1) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(test1);
        trigger.setCronExpression("2/3 * * * * ? *");
        trigger.setName("test1");

        return trigger;
    }

    /**
     * attention:
     * Details:定义quartz调度工厂
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger... triggers) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动,应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器
        bean.setTriggers(triggers);
        return bean;
    }
}

好了,至此spring boot 整合 quartz完成了,源码链接 源码

你可能感兴趣的:(spring boot quartz example demo)