环境变量中设置执行时间,
单个执行定时任务:
import com.ksyun.alertmanager.service.AlertManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
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.config.Task;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
@Component
@Configuration
@EnableScheduling
public class AlertCriticalTask implements SchedulingConfigurer {
public static String cron;
@Autowired
private AlertManagerService alertManagerService;
/**
* Callback allowing a {@link TaskScheduler
* TaskScheduler} and specific {@link Task Task}
* instances to be registered against the given the {@link ScheduledTaskRegistrar}.
*
* @param taskRegistrar the registrar to be configured.
*/
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//项目部署时,会在这里执行一次,从数据库拿到cron表达式
Map<String, String> getenv = System.getenv();
cron = getenv.get("alertmanager.critical");
//cron = "*/30 * * * * ?";
if (cron == null){
cron = "0 0 0,23 * * ?";
}
Runnable task = new Runnable() {
@Override
public void run() {
//任务逻辑代码部分.
System.out.println("*********************告警信息处理服务--30秒执行一次*********************");
try {
//执行的实现类
alertManagerService.getDataResponses();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//任务触发,可修改任务的执行周期.
//每一次任务触发,都会执行这里的方法一次,重新获取下一次的执行时间
cron = getenv.get("alertmanager.critical");
//cron = "*/30 * * * * ?";
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
};
taskRegistrar.addTriggerTask(task, trigger);
}
}
循环遍历,批量执行定时任务:
import com.ksyun.alertmanager.service.AlertManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
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.config.Task;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
@Configuration
@EnableScheduling
public class TaskList implements SchedulingConfigurer {
@Autowired
private AlertManagerService alertManagerService;
/**
* Callback allowing a {@link TaskScheduler
* TaskScheduler} and specific {@link Task Task}
* instances to be registered against the given the {@link ScheduledTaskRegistrar}.
*
* @param taskRegistrar the registrar to be configured.
*/
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
List<String> stringList = new ArrayList<>();
//项目部署时,会在这里执行一次,从数据库拿到cron表达式
Map<String, String> getenv = System.getenv();
/*cron = getenv.get("alertmanager.critical");
if (cron == null){
cron = "0 0 0,23 * * ?";
}*/
//cron = "*/30 * * * * ?";
/*stringList.add(getenv.get("alertmanager.critical"));
stringList.add(getenv.get("alertmanager.none"));
stringList.add(getenv.get("alertmanager.warning"));*/
stringList.add("*/10 * * * * ?");
stringList.add("*/30 * * * * ?");
stringList.add("*/50 * * * * ?");
stringList.forEach(cron ->{
Runnable task = new Runnable() {
@Override
public void run() {
//任务逻辑代码部分.
log.info("*********************告警信息处理服务" + cron + "级别的告警信息执行开始*********************");
try {
alertManagerService.getDataResponses(cron);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//任务触发,可修改任务的执行周期.
//每一次任务触发,都会执行这里的方法一次,重新获取下一次的执行时间
//cron = getenv.get("alertmanager.critical");
//cron = "*/30 * * * * ?";
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
};
taskRegistrar.addTriggerTask(task, trigger);
});
}
}