原文https://blog.csdn.net/ll840768874/article/details/78507286
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秒執行一次 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("動態修改定時任務參數,時間表達式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; } }
這個是定時任務調度執行器,采用的是注解的方式。首先要動態配置,要設置為@EnableScheduling,這是確保能夠動態,然后實現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(); } } }
運行測試類,查看結果,達到效果,親測可用