• 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;
        }
    }
    
    
  • 相关阅读:
    Codeforces704C. Black Widow
    Codeforces704B. Ant Man
    暑假学习日记2013/8/5
    暑假学习日记2013/7/31
    暑假学习日记2013/7/26
    暑假学习日记2013/7/25
    暑假学习日记2013/7/24
    暑假学习日记2013/7/23
    线性同余方程的解法笔记
    暑假学习日记2013/7/22
  • 原文地址:https://www.cnblogs.com/frankltf/p/10205654.html
Copyright © 2020-2023  润新知