|
|
@@ -2,7 +2,9 @@ package com.uas.search.schedule;
|
|
|
|
|
|
import com.uas.search.annotation.NotEmpty;
|
|
|
import com.uas.search.util.ExceptionUtils;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -70,9 +72,9 @@ public class TaskServiceImpl implements TaskService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean exist(@NotEmpty("code") String code) {
|
|
|
+ public boolean exist(@NotEmpty("title") String title) {
|
|
|
for (TaskInformation d : taskInformations) {
|
|
|
- if (Objects.equals(code, d.getCode())) {
|
|
|
+ if (Objects.equals(title, d.getTitle())) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
@@ -131,37 +133,55 @@ public class TaskServiceImpl implements TaskService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String start() {
|
|
|
+ public String start(TaskInformation taskInformation) {
|
|
|
String message = "";
|
|
|
-// if (!isStopped()) {
|
|
|
-// message = "已存在运行的定时任务";
|
|
|
-// logger.error(message);
|
|
|
-// return message;
|
|
|
-// }
|
|
|
- if (!CollectionUtils.isEmpty(taskInformations)) {
|
|
|
- // 线程数为1,以免多个创建索引的任务互相影响,导致索引创建失败
|
|
|
- scheduledExecutorService = Executors.newScheduledThreadPool(4);
|
|
|
- for (TaskInformation taskInformation : taskInformations) {
|
|
|
- logger.info("New task: " + taskInformation);
|
|
|
- switch (taskInformation.getScheduleType()) {
|
|
|
- case FixedRate:
|
|
|
- scheduledExecutorService.scheduleAtFixedRate(getCommand(taskInformation),
|
|
|
- taskInformation.getInitialDelay(), taskInformation.getInterval(), TimeUnit.MILLISECONDS);
|
|
|
- break;
|
|
|
- case FixedDelay:
|
|
|
- scheduledExecutorService.scheduleWithFixedDelay(getCommand(taskInformation),
|
|
|
- taskInformation.getInitialDelay(), taskInformation.getInterval(), TimeUnit.MILLISECONDS);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- message = "已开启定时任务:" + taskInformations;
|
|
|
- logger.info(message + "\n");
|
|
|
+
|
|
|
+ if (!isIdle()) {
|
|
|
+ message = "任务池已满";
|
|
|
return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (taskInformation != null) {
|
|
|
+ scheduledTask(taskInformation);
|
|
|
+ } else if (!CollectionUtils.isEmpty(taskInformations)) {
|
|
|
+ for (TaskInformation task : taskInformations) {
|
|
|
+ scheduledTask(task);
|
|
|
+ }
|
|
|
} else {
|
|
|
message = "定时任务为空";
|
|
|
logger.error(message + "\n");
|
|
|
return message;
|
|
|
}
|
|
|
+
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String scheduledTask(TaskInformation taskInformation) {
|
|
|
+ String message = "";
|
|
|
+ ScheduledExecutorService scheduledExecutorService = scheduledExecutorMap.get(taskInformation.getTitle());
|
|
|
+ if (scheduledExecutorService == null) {
|
|
|
+ // 线程数为1,以免同一索引多个的任务互相影响,导致索引创建失败
|
|
|
+ // TODO Products需要以企业多线程
|
|
|
+ scheduledExecutorService = Executors.newScheduledThreadPool(1);
|
|
|
+ logger.info("New task: " + taskInformation);
|
|
|
+ switch (taskInformation.getScheduleType()) {
|
|
|
+ case FixedRate:
|
|
|
+ scheduledExecutorService.scheduleAtFixedRate(getCommand(taskInformation),
|
|
|
+ taskInformation.getInitialDelay(), taskInformation.getInterval(), TimeUnit.MILLISECONDS);
|
|
|
+ break;
|
|
|
+ case FixedDelay:
|
|
|
+ scheduledExecutorService.scheduleWithFixedDelay(getCommand(taskInformation),
|
|
|
+ taskInformation.getInitialDelay(), taskInformation.getInterval(), TimeUnit.MILLISECONDS);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ scheduledExecutorMap.put(taskInformation.getTitle(), scheduledExecutorService);
|
|
|
+ message = "已开启定时任务:" + taskInformation;
|
|
|
+ logger.info(message + "\n");
|
|
|
+ return message;
|
|
|
+ } else {
|
|
|
+ message = "已存在运行的定时任务";
|
|
|
+ }
|
|
|
+ return message;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -190,22 +210,65 @@ public class TaskServiceImpl implements TaskService {
|
|
|
public String stop() {
|
|
|
String message = "";
|
|
|
if (isStopped()) {
|
|
|
- message = "定时任务已经停止或者未开启过";
|
|
|
+ message = "全部定时任务已经停止或者未开启过";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ logger.info("Remove old tasks...");
|
|
|
+
|
|
|
+ for (Entry entry : scheduledExecutorMap.entrySet()) {
|
|
|
+ ScheduledExecutorService scheduledExecutorService = (ScheduledExecutorService) entry.getValue();
|
|
|
+ scheduledExecutorService.shutdownNow();
|
|
|
+ }
|
|
|
+ taskInformations = new ArrayList<>();
|
|
|
+ scheduledExecutorMap = new HashMap<>();
|
|
|
+ message = "已关闭定时任务";
|
|
|
+ logger.info(message + "\n");
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String stop(TaskInformation taskInformation) {
|
|
|
+ String message = "";
|
|
|
+ if (isStopped(taskInformation)) {
|
|
|
+ message = "定时任务" + taskInformation.getTitle() + "已经停止或者未开启过";
|
|
|
logger.error(message);
|
|
|
return message;
|
|
|
}
|
|
|
logger.info("Remove old tasks...");
|
|
|
+
|
|
|
+ ScheduledExecutorService scheduledExecutorService = scheduledExecutorMap.get(taskInformation.getTitle());
|
|
|
scheduledExecutorService.shutdownNow();
|
|
|
+
|
|
|
message = "已关闭定时任务";
|
|
|
logger.info(message + "\n");
|
|
|
return message;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public boolean isStopped(TaskInformation taskInformation) {
|
|
|
+ ScheduledExecutorService scheduledExecutorService = scheduledExecutorMap.get(taskInformation.getTitle());
|
|
|
+ return scheduledExecutorService == null || scheduledExecutorService.isTerminated() || scheduledExecutorService.isShutdown();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean isStopped() {
|
|
|
- if (scheduledExecutorService == null) {
|
|
|
- return true;
|
|
|
+ if (scheduledExecutorMap.size() > 0) {
|
|
|
+ for (Entry entry : scheduledExecutorMap.entrySet()) {
|
|
|
+ ScheduledExecutorService scheduledExecutorService = (ScheduledExecutorService) entry.getValue();
|
|
|
+ if (scheduledExecutorService != null && (!scheduledExecutorService.isShutdown() || !scheduledExecutorService.isTerminated())) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isIdle() {
|
|
|
+ if (scheduledExecutorMap == null) {
|
|
|
+ scheduledExecutorMap = new HashMap<>();
|
|
|
}
|
|
|
- return scheduledExecutorService.isShutdown() || scheduledExecutorService.isTerminated();
|
|
|
+ return scheduledExecutorMap.size() < 4;
|
|
|
}
|
|
|
}
|