修改任务调度为动态
parent
729eede60e
commit
569e5370f2
@ -0,0 +1,18 @@
|
||||
package com.emr.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
/**
|
||||
* 任务调度线程池配置
|
||||
* @author
|
||||
* @date 20190127
|
||||
*/
|
||||
@Configuration
|
||||
public class ScheduleConfig {
|
||||
@Bean
|
||||
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
|
||||
return new ThreadPoolTaskScheduler();
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.emr.quart;
|
||||
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
|
||||
@Component
|
||||
public class DynamicScheduleTaskSecond {
|
||||
@Autowired
|
||||
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
|
||||
@Autowired
|
||||
private Task1 scheduleExport;
|
||||
private ScheduledFuture<?> future;
|
||||
|
||||
public void setCron(String cron) {
|
||||
stopCron();
|
||||
future = threadPoolTaskScheduler.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
scheduleExport.task1();// 执行任务
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, new Trigger() {
|
||||
@Override
|
||||
public Date nextExecutionTime(TriggerContext triggerContext) {
|
||||
if ("".equals(cron) || cron == null) {
|
||||
return null;
|
||||
}
|
||||
CronTrigger trigger = new CronTrigger(cron);// 定时任务触发,可修改定时任务的执行周期
|
||||
Date nextExecDate = trigger.nextExecutionTime(triggerContext);
|
||||
return nextExecDate;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stopCron() {
|
||||
if (future != null) {
|
||||
future.cancel(true);//取消任务调度
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue