@Scheduled 如何讀取動態(tài)配置文件
@Scheduled讀取動態(tài)配置文件
application.yml配置文件得配置信息
agreeAccTask: # # 每3分鐘執(zhí)行一次,handTime: 0 0/3 * * * ? 每天晚上2點 handTime: 0 0 2 * * ? # 指定幾天內: day 1 表示當前天內,2表示二天內,依次類推 # 指定生成目錄路勁: dir F:/agreeacc time: 0 0 2 * * ? day: 1 dir: F:/agreeacc3
java后端
* @Description:支付寶代扣對賬文件定時生成
*/
@Slf4j
@Component
@EnableScheduling
public class AgreeAccTask {
@Autowired
private AgreeAccMapper agreeAccMapper;
@Value("${agreeAccTask.day}")
private String day;
@Value("${agreeAccTask.dir}")
private String dir;
@Scheduled(cron="${agreeAccTask.time}")
public void AgreeAccTask(){
String today=DateUtil.date2String(new Date(),DateUtil.PATTERN_DATE);
log.info("【生成代扣對賬文件開始】日期:"+today);
int j=Integer.parseInt(day);
SpringBoot @Scheduled 讀取配置文件獲取cron值
1、在類上加注解@Component,交給Spring管理
2、在@PropertySource指定配置文件名稱,如下配置,指定放在src/main/resource目錄下的application.properties文件
3、配置文件application.properties參考內容如下
#每秒鐘執(zhí)行一次 cron=0/1 * * * * *
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:/application.properties")
public class ScoreTask1 {
@Scheduled(cron="${cron}")
public void scoreTask(){
System.out.println("從配置文件讀取cron表達式定時器");
}
}
進一步衍生,可以把開發(fā)環(huán)境跟生產環(huán)境cron表達式做環(huán)境隔離,這樣每次打包部署,系統(tǒng)自動執(zhí)行配置文件cron表達式,減少手動修改代碼次數。
這里推薦一個在線生成cron表達式的工具 在線Cron表達式生成器
https://cron.qqe2.com/
注意cron表達式只能配置6位參數,即 秒分時 日月周,有時候生成器會生成7位參數,這樣在java代碼執(zhí)行會報錯的
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android?Studio中創(chuàng)建java工程的完整步驟
Android?Studio創(chuàng)建java工程是非常麻煩的,因為Android?Studio沒有提供直接創(chuàng)建java工程的方法,下面這篇文章主要給大家介紹了關于Android?Studio中創(chuàng)建java工程的完整步驟,需要的朋友可以參考下2024-01-01
java 中sendredirect()和forward()方法的區(qū)別
這篇文章主要介紹了java 中sendredirect()和forward()方法的區(qū)別,需要的朋友可以參考下2017-08-08
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應用程序錯誤解決
這篇文章主要給大家介紹了關于java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應用程序錯誤的解決方法,文中介紹的非常詳細,需要的朋友們可以參考學習,下面來一起看看吧。2017-03-03

