使用spring-task定時任務動態(tài)配置修改執(zhí)行時間
spring-task定時任務動態(tài)配置修改執(zhí)行時間
因項目需要,幾個定時任務需要人為動態(tài)設置執(zhí)行時間,于是乎吧,就查閱相關資料,是可以動態(tài)設置的,廢話不多說,直接上代碼,一目了然。
package com.seckill.quartz;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by loup on 2017/11/11.
*/
@Component
@EnableScheduling
public class DynamicScheduledTask implements SchedulingConfigurer {
//時間表達式 每2秒執(zhí)行一次
private String cron = "0/2 * * * * ?";
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
//任務邏輯
System.out.println("---------------start-------------------");
System.out.println("動態(tài)修改定時任務參數(shù),時間表達式cron為:" + cron);
System.out.println("當前時間為:" + sdf.format(new Date()));
System.out.println("----------------end--------------------");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
CronTrigger cronTrigger = new CronTrigger(cron);
Date nextExecDate = cronTrigger.nextExecutionTime(triggerContext);
return nextExecDate;
}
});
}
public void setCron(String cron) {
this.cron = cron;
}
}
這個是定時任務調度執(zhí)行器,采用的是注解的方式。首先要動態(tài)配置,要設置為@EnableScheduling,這是確保能夠動態(tài),然后實現(xiàn)SchedulingConfigurer,重寫configureTasks方法,接下來就是這個的相關spring配置文件,要引入下面這個task,不然識別不了啊,配置文件就是這么簡單
http://www.springframework.org/schema/task
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.seckill.quartz"/>
<task:annotation-driven />
</beans>
接下來就是寫測試類,測試可不可行啊
package com.seckill.quartz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
/**
* Created by loup on 2017/11/11.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/conf/spring-quartz.xml"})
public class QuartzTest {
@Autowired
private DynamicScheduledTask dynamicScheduledTask;
@Test
public void test1(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dynamicScheduledTask.setCron("0/10 * * * * ?");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行測試類,查看結果,達到效果,親測可用
spring schedule 動態(tài)配置執(zhí)行時間
之前saas平臺實現(xiàn)動態(tài)修改定時任務的時間,都是通過xx-job這樣的框架來實現(xiàn),這樣我們可以單獨一個服務來管理我們整個saas平臺的定時任務,但是最近給銀行做的一個小項目,需要本地化部署,所以我不想弄很多的服務,并且他們并沒有要求修改以后即時生效,所以我直接采用了 spring schedule結合mysql動態(tài)配置執(zhí)行時間。
之前我們用的schedule通過注解的方式,只能用靜態(tài)的corn表達式,如果想實現(xiàn)動態(tài)的需要實現(xiàn)SchedulingConfigurer,并且通過注解@EnableScheduling。如下:
package com.zqf.marketing.task;
import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch;
import com.zqf.marketing.sys.service.SwitchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author zhenghao
* @description
* @date 2019/1/22 21:50
*/
@Lazy(false)
@Service
@EnableScheduling
public class TestTaskService implements SchedulingConfigurer {
private static Logger log = LoggerFactory.getLogger(TestTaskService.class);
@Autowired
private SwitchService switchService;
private String SpringDynamicCronTask() {
String cron = "0/5 * * * * ?";
//從數(shù)據庫獲得配置的corn表達式
RobotSysSwitch switchById = switchService.getSwitchById(5L);
cron = switchById.getSwitchFlag();
log.info(cron);
return cron;
}
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
// 任務邏輯
log.info("task_task_tak");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String s = SpringDynamicCronTask();
// 任務觸發(fā),可修改任務的執(zhí)行周期
CronTrigger trigger = new CronTrigger(s);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
這樣我們就可以動態(tài)的修改task的執(zhí)行時間,生效時間為,上一個任務的執(zhí)行周期,也可以滿足我們現(xiàn)在需求,這樣就可以實習項目更加的靈活!
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springsecurity記住我登錄時訪問無權限接口跳轉登錄界面的處理方案
這篇文章主要介紹了springsecurity記住我登錄時訪問無權限接口跳轉登錄界面的處理方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-02-02
Java五種方式實現(xiàn)多線程循環(huán)打印問題
本文主要介紹了Java五種方式實現(xiàn)多線程循環(huán)打印問題,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
spring?和?idea?建議不要使用?@Autowired注解的原因解析
@Autowired?是Spring框架的注解,而@Resource是JavaEE的注解,這篇文章主要介紹了spring和idea建議不要使用@Autowired注解的相關知識,需要的朋友可以參考下2023-11-11
k8s+springboot+CronJob定時任務部署實現(xiàn)
本文主要介紹了k8s+springboot+CronJob定時任務部署實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07

