SpringBoot使用Quartz定时任务———以微信步数为例

SpringBoot使用Quartz定时任务———以微信步数为例

Quartz:这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。
话不多说看代码

pom依赖


        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.projectlombok
            lombok
            1.16.10
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        


        
            org.webjars
            bootstrap
            3.3.7-1
        

        
            org.springframework.boot
            spring-boot-devtools
            true
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            mysql
            mysql-connector-java
        


        
            org.springframework.boot
            spring-boot-starter-quartz
        

        
            com.spring4all
            swagger-spring-boot-starter
            1.8.0.RELEASE
        

        
            com.github.caspar-chen
            swagger-ui-layer
            0.0.6
        
    

properties配置

server.port=8080
spring.thymeleaf.cache=false


spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot_quartz?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##连接池中最大的活跃连接数
spring.datasource.tomcat.max-active=20  
##连接池中最大、最小的空闲连接数
spring.datasoure.max-idle=8
spring.datasoure.min-idle=8
##初始化连接数
spring.datasoure.initial=10



spring.jpa.database=mysql
# 显示SQL语句
spring.jpa.show-sql=true
##指定DDL mode (none, validate, update, create, create-drop)
spring.jpa.properties.hibernate.hbm2ddl.auto=update

QuartzConfig

@Configuration
public class QuartzConfig {
    @Autowired
    private SpringJobFactory springJobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(springJobFactory);
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }
}

JobFactory

@Component
public class SpringJobFactory extends AdaptableJobFactory {

    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

更多点击查看git代码

你可能感兴趣的:(SpringBoot使用Quartz定时任务———以微信步数为例)