|
|
|
@ -1,45 +1,81 @@
|
|
|
|
|
package ${basePackage}.config;
|
|
|
|
|
|
|
|
|
|
import ${basePackage}.action.GlobalController;
|
|
|
|
|
import ${basePackage}.frame.utils.LogUtil;
|
|
|
|
|
import ${basePackage}.frame.utils.ProcessUtil;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
|
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
import org.springframework.scheduling.Trigger;
|
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import ${basePackage}.frame.utils.LogUtil;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Task配置,默认只在开发环境生效,根据实际情况是否需要Task选择开启
|
|
|
|
|
* 开启方法:将@Profile("dev") 删除(所有环境生效) 或改为 @Profile("prod")(只对生产环境生效)
|
|
|
|
|
* 开启方法:将@Profile("dev") 删除(所有环境生效) 或改为 (只对生产环境生效)
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
@EnableScheduling
|
|
|
|
|
@Component
|
|
|
|
|
@Profile("dev")
|
|
|
|
|
public class TaskConfig implements SchedulingConfigurer {
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron="0/5 * * * * ? ")
|
|
|
|
|
public void task(){
|
|
|
|
|
// todo 自定实现的定时任务
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
|
HashMap<String, String> data = new HashMap<>();
|
|
|
|
|
data.put("time", format.format(new Date()));
|
|
|
|
|
GlobalController.pushAll(data);
|
|
|
|
|
public class TaskConfig extends ThreadPoolTaskScheduler {
|
|
|
|
|
|
|
|
|
|
public TaskConfig() {
|
|
|
|
|
futureMap = new HashMap<>();
|
|
|
|
|
setPoolSize(4);
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void ss() {
|
|
|
|
|
TaskConfig.this.addTask(1L, new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
|
LogUtil.d(format.format(new Date()));
|
|
|
|
|
}
|
|
|
|
|
}, new CronTrigger("0/3 * * * * ? "));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spring的 Schedule 默认是单线程执行
|
|
|
|
|
* 若要多线程并行运行任务 需实现SchedulingConfigurer并重写configureTasks方法
|
|
|
|
|
*
|
|
|
|
|
* @param scheduledTaskRegistrar
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
|
|
|
|
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));
|
|
|
|
|
private Map<String, ScheduledFuture<?>> futureMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean addTask(Long taskId, Runnable runnable, Trigger trigger) {
|
|
|
|
|
ScheduledFuture<?> schedule = this.schedule(runnable, trigger);
|
|
|
|
|
futureMap.put(taskId.toString(), schedule);
|
|
|
|
|
System.out.println("DynamicTask.startCron()");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public void configureTasks(ScheduledTaskRegistrar registrar) {
|
|
|
|
|
// // 开启新线程模拟外部更改了任务执行周期
|
|
|
|
|
// new Thread(new Runnable() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public void run() {
|
|
|
|
|
// try {
|
|
|
|
|
// Thread.sleep(15 * 1000);
|
|
|
|
|
// } catch (InterruptedException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// cron = "0/10 * * * * ? ";
|
|
|
|
|
// System.err.println("cron change to: " + cron);
|
|
|
|
|
// }
|
|
|
|
|
// }).start();
|
|
|
|
|
|
|
|
|
|
// this.registrar = registrar;
|
|
|
|
|
// this.registrar.setScheduler(Executors.newScheduledThreadPool(3));
|
|
|
|
|
// CronTrigger cronTrigger = new CronTrigger(cron) {
|
|
|
|
|
//// @Override
|
|
|
|
|
//// public Date nextExecutionTime(TriggerContext triggerContext) {
|
|
|
|
|
//// // 任务触发,可修改任务的执行周期
|
|
|
|
|
//// CronTrigger trigger = new CronTrigger(cron);
|
|
|
|
|
//// return trigger.nextExecutionTime(triggerContext);
|
|
|
|
|
//// }
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|