• spring -boot定时任务 quartz 基于 JobDetailFactoryBean实现


      这个有点小问题 尚未解决  后期优化

    基于 JobDetailFactoryBean实现

    依赖包

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-quartz</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    View Code

    配置类

    package quarttest.demo;
    
    import org.quartz.JobDataMap;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
    import org.springframework.scheduling.quartz.JobDetailFactoryBean;
    
    
    @Configuration
    public class QuartzConfig {
    
        /**
         *  在 Quartz  配置类中,主要配置两个东西:1. JobDetail 2. Trigger
         *   JobDetail 有两种不同的配置方式:
         *  * 1. MethodInvokingJobDetailFactoryBean   此处是基于方法1 的 即 MethodInvokingJobDetailFactoryBean
         *  * 2. JobDetailFactoryBean
         * @return
         */
    
        @Bean
        JobDetailFactoryBean jobDetailFactoryBean() {
            JobDetailFactoryBean bean = new JobDetailFactoryBean();
            bean.setJobClass(MyJob2.class);
            JobDataMap map = new JobDataMap();
            map.put("helloService", helloService());
            bean.setJobDataMap(map);
            return bean;
        }
    
        @Bean
        CronTriggerFactoryBean cronTrigger() {
            CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
            bean.setCronExpression("0/10 * * * * ?"); 
            bean.setJobDetail(jobDetailFactoryBean().getObject());
            return bean;
        }
    
        @Bean
        HelloService helloService() {
            return new HelloService();
        }
    
    
    }
    View Code

    任务类

    package quarttest.demo;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyJob2 extends QuartzJobBean {
        HelloService helloService;
    
        public HelloService getHelloService() {
            return helloService;
        }
    
        public void setHelloService(HelloService helloService) {
            this.helloService = helloService;
        }
    
        @Override
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
            helloService.sayHello();
        }
    }
    View Code

    具体实现类

    package quarttest.demo;
    
    import java.util.Date;
    
    public class HelloService {
        public void sayHello() {
            System.out.println("hello service >>>"+new Date());
        }
    }
    View Code
  • 相关阅读:
    小白学 Python(11):基础数据结构(元组)
    小白学 Python(10):基础数据结构(列表)(下)
    小白学 Python(9):基础数据结构(列表)(上)
    小白学 Python(8):基础流程控制(下)
    小白学 Python(7):基础流程控制(上)
    小白学 Python(6):基础运算符(下)
    小白学 Python(5):基础运算符(上)
    小白学 Python(4):变量基础操作
    小白学 Python(3):基础数据类型(下)
    小白学 Python(2):基础数据类型(上)
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11250555.html
Copyright © 2020-2023  润新知