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

时间:2021-10-14 21:38:44

spring 定时任务 quartz 基于  MethodInvokingJobDetailFactoryBean 实现 

 

依赖包 如下

spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现spring -boot定时任务 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

spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现
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

 


任务类

spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现
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