|
@@ -1,179 +0,0 @@
|
|
|
-package com.nokia.tsl_data.service;
|
|
|
-
|
|
|
-import com.nokia.common.spring.context.SpringContextHolder;
|
|
|
-import com.nokia.tsl_data.dao.ScheduledTaskMapper;
|
|
|
-import com.nokia.tsl_data.entity.ScheduledTask;
|
|
|
-import com.nokia.tsl_data.entity.pojo.RunnableTask;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.scheduling.Trigger;
|
|
|
-import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
-import org.springframework.scheduling.support.CronTrigger;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
-import java.io.IOException;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Paths;
|
|
|
-import java.nio.file.StandardOpenOption;
|
|
|
-import java.time.Duration;
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.ScheduledFuture;
|
|
|
-
|
|
|
-/**
|
|
|
- * 任务注册服务
|
|
|
- * 1. 启动时初始化任务
|
|
|
- * 2. 关闭时保存未完成任务
|
|
|
- * 3. 任务调度
|
|
|
- * 4. 任务取消
|
|
|
- * 5. 任务列表
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class SchedulingTaskService {
|
|
|
-
|
|
|
- // 使用id作为任务唯一标识
|
|
|
- private final ConcurrentHashMap<ScheduledTask, ScheduledFuture<?>> tasksScheduled = new ConcurrentHashMap<>();
|
|
|
- private final ThreadPoolTaskScheduler taskScheduler;
|
|
|
- private final ScheduledTaskMapper scheduledTaskMapper;
|
|
|
- private final SpringContextHolder springContextHolder;
|
|
|
-
|
|
|
- public SchedulingTaskService(ThreadPoolTaskScheduler taskScheduler, ScheduledTaskMapper scheduledTaskMapper, SpringContextHolder springContextHolder) {
|
|
|
- this.taskScheduler = taskScheduler;
|
|
|
- this.scheduledTaskMapper = scheduledTaskMapper;
|
|
|
- this.springContextHolder = springContextHolder;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 增加任务
|
|
|
- */
|
|
|
- public void addTask(ScheduledTask scheduledTask) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新任务
|
|
|
- */
|
|
|
- public void updateTask(ScheduledTask scheduledTask) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除任务
|
|
|
- */
|
|
|
- public void deleteTask(ScheduledTask scheduledTask) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 调度任务
|
|
|
- */
|
|
|
- private void scheduleTask(ScheduledTask scheduledTask) {
|
|
|
- if (tasksScheduled.containsKey(scheduledTask)) {
|
|
|
- throw new RuntimeException("已存在相同任务,请勿重复调度...");
|
|
|
- }
|
|
|
- Runnable runnable = new RunnableTask(springContextHolder, scheduledTask);
|
|
|
- if (scheduledTask.getStatus()) {
|
|
|
- ScheduledFuture<?> future = null;
|
|
|
- switch (scheduledTask.getScheduledType()) {
|
|
|
- case CRON:
|
|
|
- // 调度定时任务
|
|
|
- Trigger trigger = new CronTrigger(scheduledTask.getScheduledParameter().getCronExpression());
|
|
|
- future = taskScheduler.schedule(runnable, trigger);
|
|
|
- break;
|
|
|
- case INTERVAL:
|
|
|
- // 周期任务
|
|
|
- Date startTime = scheduledTask.getScheduledParameter().getStartTime();
|
|
|
- Duration period = Duration.ofSeconds(scheduledTask.getScheduledParameter().getPeriodOfSeconds());
|
|
|
- if (startTime == null || startTime.getTime() <= System.currentTimeMillis()) {
|
|
|
- //
|
|
|
- future = taskScheduler.scheduleAtFixedRate(runnable, period);
|
|
|
- } else {
|
|
|
- future = taskScheduler.scheduleAtFixedRate(runnable, startTime.toInstant(), period);
|
|
|
- }
|
|
|
- break;
|
|
|
- case ONCE:
|
|
|
- // 单次任务
|
|
|
- Date startTimeForOnceTask = scheduledTask.getScheduledParameter().getStartTime();
|
|
|
- if (startTimeForOnceTask.getTime() >= System.currentTimeMillis()) {
|
|
|
- // 单次任务只有在启动时间大于等于当前时间时才启动调度
|
|
|
- future = taskScheduler.schedule(runnable, startTimeForOnceTask);
|
|
|
- } else {
|
|
|
- // 当启动时间早于当前时间时,直接报错
|
|
|
- throw new RuntimeException("单次任务(ONCE)调度时间不能早于当前时间...");
|
|
|
- }
|
|
|
- break;
|
|
|
- case IMMEDIATELY:
|
|
|
- // 马上执行的任务
|
|
|
- default:
|
|
|
- future = taskScheduler.schedule(runnable, Instant.now());
|
|
|
- break;
|
|
|
- }
|
|
|
- if (future != null) {
|
|
|
- tasksScheduled.put(scheduledTask, future);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 列出已注册调度任务的列表
|
|
|
- */
|
|
|
- public List<ScheduledTask> listTasksScheduled() {
|
|
|
- List<ScheduledTask> result = new ArrayList<>();
|
|
|
- // 遍历所有正在调度的任务
|
|
|
- for (Map.Entry<ScheduledTask, ScheduledFuture<?>> entry : tasksScheduled.entrySet()) {
|
|
|
- ScheduledFuture<?> future = entry.getValue();
|
|
|
- if (future.isDone()) {
|
|
|
- tasksScheduled.remove(entry.getKey());
|
|
|
- } else {
|
|
|
- result.add(entry.getKey());
|
|
|
- }
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 取消任务
|
|
|
- */
|
|
|
- public void cancelTask(ScheduledTask scheduledTask) {
|
|
|
- ScheduledFuture<?> future = tasksScheduled.remove(scheduledTask);
|
|
|
- if (future != null) {
|
|
|
- future.cancel(true);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 在启动时获取需要调度的任务并进行调度
|
|
|
- */
|
|
|
- @PostConstruct
|
|
|
- public void postConstruct() {
|
|
|
- log.info("启动时初始化任务...");
|
|
|
- List<ScheduledTask> scheduledTasks = scheduledTaskMapper.selectTasksIsOn();
|
|
|
- for (ScheduledTask scheduledTask : scheduledTasks) {
|
|
|
- try {
|
|
|
- scheduleTask(scheduledTask);
|
|
|
- }catch (Exception e) {
|
|
|
- log.info("初始化任务 {} 出错: {}", scheduledTask, e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 在退出程序前保存程序调度的状态
|
|
|
- */
|
|
|
- @PreDestroy
|
|
|
- public void preDestroy() throws IOException {
|
|
|
- // TODO
|
|
|
- log.info("============preDestroy===========");
|
|
|
- List<String> lines = new ArrayList<>();
|
|
|
- lines.add("=======preDestroy========");
|
|
|
- lines.add(String.valueOf(System.currentTimeMillis()));
|
|
|
- log.info(Paths.get("./output/aaa.text").toAbsolutePath().toString());
|
|
|
- Files.write(Paths.get("./output/aaa.text"), lines, StandardOpenOption.APPEND);
|
|
|
- }
|
|
|
-}
|