其他分享
首页 > 其他分享> > Springcloud学习笔记43--任务调度框架Quartz 使用02

Springcloud学习笔记43--任务调度框架Quartz 使用02

作者:互联网

1.pom 依赖配置

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.3.16</version>
        </dependency>

2.注册Quartz 任务(Job) 工厂和注册调度工厂

2.1 注册Quartz 任务(Job) 工厂

package com.ttbank.flep.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * @Author lucky
 * @Date 2022/4/1 15:50
 * 注册Quartz 任务(Job) 工厂
 */
@Component
public class QuartzJobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory; //AutowireCapableBeanFactory提供了自动装配Bean的实现,提供bean创建(带有构造函数解析)、属性填充、连接(包括自动装配)和初始化

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        //调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        //进行注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

2.2 配置数据库连接池

(1)读取yaml文件中的配置项

package com.ttbank.flep.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author lucky
 * @Date 2022/3/31 10:22
 */
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DataSourceProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private int initialSize;
    private int minIdle;
    private int maxActive=100;
    private long maxWait;
    private long timeBetweenEvictionRunsMillis;
    private long minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;

}

(2)

标签:02,Quartz,String,private,springframework,org,import,任务调度
来源: https://www.cnblogs.com/luckyplj/p/16111584.html