码迷,mamicode.com
首页 > 编程语言 > 详细

Spring boot 整合 Quartz (小白专场)

时间:2019-11-07 18:52:24      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:triggers   work   object   public   开始   contex   util   ons   char   

0.项目结构(含主函数):
技术图片

1.依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xinyu.test</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>RELEASE</version>
            <exclusions>
               <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-logging</artifactId>
               </exclusion>
            </exclusions>

        </dependency>

    </dependencies>

</project> 

2.定时器配置代码:

package com.quartz;


import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;



@Configuration
@Component
public class QuartzConfig {


    @Bean(name = "quartzTaskBean")
    public MethodInvokingJobDetailFactoryBean chargingFeedBackBean(QuartzTask task){
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发
        jobDetail.setConcurrent(false);
        // 任务的名字
        jobDetail.setName("quartzTask");
        // 任务的分组
        jobDetail.setGroup("feedBack");
        // 被执行的对象
        jobDetail.setTargetObject(task);
        // 被执行的方法
        jobDetail.setTargetMethod("doTask");
        return jobDetail;
    }


    /**
     //     * 表达式触发器工厂Bean
     //     */
    @Bean(name = "quartzTaskTrigger")
    public CronTriggerFactoryBean jobTestTaskTrigger(@Qualifier("quartzTaskBean") MethodInvokingJobDetailFactoryBean quartzTaskBean) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(quartzTaskBean.getObject());
        // 什么是否触发,Spring Scheduler Cron表达式,每秒一次
        trigger.setCronExpression("0/1 * * * * ?");
        trigger.setName("quartzTaskTrigger");
        return trigger;
    }






    /**
     //     * 调度器工厂Bean
     //     */
    @Bean(name = "schedulerFactory")
//    public SchedulerFactoryBean schedulerFactory(@Qualifier("agvErrorReportTrigger") Trigger agvErrorReportTrigger,
//                                                 @Qualifier("agvChargingReportTrigger")Trigger agvChargingReportTrigger) {
    public SchedulerFactoryBean schedulerFactory(@Qualifier("quartzTaskTrigger") Trigger quartzTaskTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 覆盖已存在的任务
        bean.setOverwriteExistingJobs(false);
        //项目启动完成后,等待5秒后开始执行调度器初始化
        bean.setOverwriteExistingJobs(true);
        // 延时启动定时任务,避免系统未完全启动却开始执行定时任务的情况
        bean.setStartupDelay(10);
        // 判断定时任务是否开器
        List<Trigger> triggers = new ArrayList<>();

        triggers.add(quartzTaskTrigger);

        Trigger[] ts = triggers.toArray(new Trigger[0]);
        // 注册触发器
        bean.setTriggers(ts);
        return bean;
    }

}

3.业务代码:

package com.quartz;

import org.springframework.stereotype.Component;

@Component
public class QuartzTask {



    public void doTask() {
        System.out.println("成功调用");
    }
}

4.执行结果:

技术图片

Spring boot 整合 Quartz (小白专场)

标签:triggers   work   object   public   开始   contex   util   ons   char   

原文地址:https://www.cnblogs.com/white-123/p/11814088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!