1. 程式人生 > 其它 >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)