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

时间:2022-02-08 07:59:41

 

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

基于 JobDetailFactoryBean实现

依赖包

 

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

 

配置类

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

任务类

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

具体实现类

spring -boot定时任务 quartz 基于 JobDetailFactoryBean实现spring -boot定时任务 quartz 基于 JobDetailFactoryBean实现
package quarttest.demo;

import java.util.Date;

public class HelloService {
    public void sayHello() {
        System.out.println("hello service >>>"+new Date());
    }
}
View Code