zoukankan      html  css  js  c++  java
  • spring项目如何使用quartz执行多个定时任务

    依赖

    <!--   定时器   -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
         </dependency>

    配置文件

    package com.sundear.demo;

    import com.sundear.Timing.*;
    import org.quartz.JobDetail;
     
    @Configuration
    public class QuartzConfiguration {

        // 配置定时任务1
        @Bean(name = "firstJobDetail")
        public MethodInvokingJobDetailFactoryBean firstJobDetail(TimingByDay timingByDay) {
            MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
            // 是否并发执行
            jobDetail.setConcurrent(true);
            // 为需要执行的实体类对应的对象
            jobDetail.setTargetObject(timingByDay);
            // 需要执行的方法
            jobDetail.setTargetMethod("task");
            return jobDetail;
        }

        // 配置触发器1
        @Bean(name = "firstTrigger")
        public CronTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
            CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
          trigger.setJobDetail(firstJobDetail);
          // 设置定时时间
            trigger.setCronExpression("0 46 15 * * ? 2020");
            return trigger;
        }

        // 配置定时任务2
        @Bean(name = "secondJobDetail")
        public MethodInvokingJobDetailFactoryBean secondJobDetail(TimingByWeek timingByWeek) {
            MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
            // 是否并发执行
            jobDetail.setConcurrent(true);
            // 为需要执行的实体类对应的对象
            jobDetail.setTargetObject(timingByWeek);
            // 需要执行的方法
            jobDetail.setTargetMethod("task");
            return jobDetail;
        }

        // 配置触发器2
        @Bean(name = "secondTrigger")
        public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
            CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
            trigger.setJobDetail(secondJobDetail);
            trigger.setCronExpression("0 24 19 * * ? 2020");
            return trigger;
        }

        // 配置Scheduler
        @Bean(name = "scheduler")
        public SchedulerFactoryBean schedulerFactory
        (Trigger firstTrigger, Trigger secondTrigger,Trigger thirdTrigger,
         Trigger forthTrigger,Trigger fifthTrigger) {
            SchedulerFactoryBean bean = new SchedulerFactoryBean();
            // 延时启动,应用启动5秒后
            bean.setStartupDelay(5);
            // 注册触发器
            bean.setTriggers(firstTrigger,secondTrigger,thirdTrigger);
            return bean;
        }
    }

    把定时任务绑定在对应的代码上

    @Component
    @EnableScheduling

    public class TimingByDay {

    //要定时执行的代码

    }

    @Component
    @EnableScheduling

    public class TimingByWeek {

    //要定时执行的代码

    }

    springboot项目启动类需要加上 @EnableScheduling,启动定时任务

  • 相关阅读:
    android 发送短信 怎样做到一条一条的发送,仅仅有在上一条发送成功之后才发送下一条短信
    qt学习笔记(五) QGraphicsPixmapItem与QGraphicsScene的编程实例 图标拖动渐变效果
    C小加 之 随机数
    垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
    WIZnet推出串口转以太网模块WIZ550S2E
    java里,当long与上了int
    几个常见字符串处理函数的实现原理
    Android平台调用Web Service:演示样例
    怎样学好游戏编程
    void及void指针含义的深刻解析
  • 原文地址:https://www.cnblogs.com/yxj808/p/13372775.html
Copyright © 2011-2022 走看看