zoukankan      html  css  js  c++  java
  • spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现

    spring 定时任务 quartz 基于  MethodInvokingJobDetailFactoryBean 实现 

    依赖包 如下

    <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

    任务配置类 QuartzConfig

    package quarttest.demo;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
    import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
    
    import java.util.Date;
    
    @Configuration
    public class QuartzConfig {
    
        /**
         *  在 Quartz  配置类中,主要配置两个东西:1. JobDetail 2. Trigger
         *   JobDetail 有两种不同的配置方式:
         *  * 1. MethodInvokingJobDetailFactoryBean   此处是基于方法1 的 即 MethodInvokingJobDetailFactoryBean
         *  * 2. JobDetailFactoryBean
         * @return
         */
    
        //第一个定时任务
        @Bean
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
            MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
            bean.setTargetBeanName("myJob1");
            bean.setTargetMethod("sayHello");
            return bean;
        }
    
    
        @Bean
        SimpleTriggerFactoryBean simpleTriggerFactoryBean() {
            SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
            bean.setStartTime(new Date());
            //  bean.setRepeatCount(5);    设置重复次数
            bean.setJobDetail(methodInvokingJobDetailFactoryBean2().getObject());
            bean.setRepeatInterval(3000);  //毫秒
            return bean;
        }
    
    
    
        //第二个定时任务
    
        @Bean
        MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean2() {
            MethodInvokingJobDetailFactoryBean bean2 = new MethodInvokingJobDetailFactoryBean();
            bean2.setTargetBeanName("myJob1");
            bean2.setTargetMethod("Hello");
            return bean2;
        }
    
    
    
        @Bean
        SimpleTriggerFactoryBean simpleTriggerFactoryBean2() {
            SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
            bean.setStartTime(new Date());
            //  bean.setRepeatCount(5);    设置重复次数
            bean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
            bean.setRepeatInterval(3000);
            return bean;
        }
    }
    View Code

    任务类

    package quarttest.demo;
    
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class MyJob1 {
        //任务一
        public void sayHello() {
            System.out.println("MyJob1>>>"+new Date());
        }
    
    
        //任务二
        public void Hello() {
            System.out.println("Hello========== "+new Date());
        }
    }
    View Code
    
    
    
     
  • 相关阅读:
    Mac 修改hosts
    MTK平台系统稳定性分析
    由MTK平台 mtkfb 设备注册疑问引发的知识延伸--ARM Device Tree
    mtk display 架构
    【6572】关于mtk平台display模块的学习探讨
    如何用Bugzilla系统管理产品研发过中相关需求和bug
    Bugzilla 系统企业应用案例
    MT6753平台一项目不同手机最低亮度存偏差问题分析过程
    嵌入式驱动实习生试题(答案)
    嵌入式驱动实习生试题
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11250287.html
Copyright © 2011-2022 走看看