|
|
@@ -1,6 +1,5 @@
|
|
|
package com.uas.search.console.b2b.schedule;
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -13,10 +12,12 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.search.b2b.exception.SearchException;
|
|
|
import com.uas.search.console.b2b.core.util.PathUtils;
|
|
|
import com.uas.search.console.b2b.schedule.model.DailyTaskInformation;
|
|
|
import com.uas.search.console.b2b.schedule.model.DailyTaskLog;
|
|
|
@@ -27,51 +28,50 @@ import com.uas.search.console.b2b.schedule.model.DailyTaskLog;
|
|
|
* @author sunyj
|
|
|
* @since 2016年12月12日 上午11:40:00
|
|
|
*/
|
|
|
-public class DailyTaskManager {
|
|
|
+@Service
|
|
|
+public class DailyTaskServiceImpl implements DailyTaskService {
|
|
|
|
|
|
private List<DailyTaskInformation> dailyTaskInformations = new ArrayList<>();
|
|
|
|
|
|
private ScheduledExecutorService scheduledExecutorService;
|
|
|
|
|
|
- private Logger logger = LoggerFactory.getLogger(DailyTaskManager.class);
|
|
|
+ private Logger logger = LoggerFactory.getLogger(DailyTaskServiceImpl.class);
|
|
|
|
|
|
/**
|
|
|
* 一天的毫秒数
|
|
|
*/
|
|
|
private static final long MILLISECONDS_OF_ONE_DAY = 24 * 60 * 60 * 1000;
|
|
|
|
|
|
- /**
|
|
|
- * 建立每天定时任务
|
|
|
- *
|
|
|
- * @param dailyTaskInformation
|
|
|
- * 每天定时任务的信息
|
|
|
- */
|
|
|
+ @Override
|
|
|
public void newDailyTask(DailyTaskInformation dailyTaskInformation) {
|
|
|
if (dailyTaskInformation == null || StringUtils.isEmpty(dailyTaskInformation.getTitle())
|
|
|
|| dailyTaskInformation.getCommand() == null) {
|
|
|
throw new NullPointerException();
|
|
|
}
|
|
|
+ if(containsDailyTask(dailyTaskInformation)){
|
|
|
+ throw new SearchException("任务已存在:"+dailyTaskInformation);
|
|
|
+ }
|
|
|
dailyTaskInformations.add(dailyTaskInformation);
|
|
|
- recreateTasks();
|
|
|
+ stop();
|
|
|
+ start();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 重新创建所有任务
|
|
|
+ * 判断定时任务是否已经存在
|
|
|
+ *
|
|
|
+ * @param dailyTaskInformation
|
|
|
+ * @return
|
|
|
*/
|
|
|
- private void recreateTasks() {
|
|
|
- if (scheduledExecutorService != null) {
|
|
|
- logger.info("Remove old daily tasks...");
|
|
|
- scheduledExecutorService.shutdownNow();
|
|
|
+ private boolean containsDailyTask(DailyTaskInformation dailyTaskInformation) {
|
|
|
+ if (CollectionUtils.isEmpty(dailyTaskInformations)) {
|
|
|
+ return false;
|
|
|
}
|
|
|
- if (!CollectionUtils.isEmpty(dailyTaskInformations)) {
|
|
|
- // 线程数与任务数保持一致,这样保证任务间不会互相影响
|
|
|
- scheduledExecutorService = Executors.newScheduledThreadPool(dailyTaskInformations.size());
|
|
|
- for (DailyTaskInformation dailyTaskInformation : dailyTaskInformations) {
|
|
|
- logger.info("New daily task: " + dailyTaskInformation);
|
|
|
- newDailyTask(dailyTaskInformation.getCommand(), dailyTaskInformation.getHour(),
|
|
|
- dailyTaskInformation.getMinute(), dailyTaskInformation.getSecond());
|
|
|
+ for (DailyTaskInformation d : dailyTaskInformations) {
|
|
|
+ if (dailyTaskInformation.equals(d)) {
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -102,11 +102,7 @@ public class DailyTaskManager {
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 保存日志到本地文件
|
|
|
- *
|
|
|
- * @param log
|
|
|
- */
|
|
|
+ @Override
|
|
|
public void saveLog(DailyTaskLog log) {
|
|
|
if (log == null) {
|
|
|
throw new NullPointerException();
|
|
|
@@ -130,4 +126,58 @@ public class DailyTaskManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<DailyTaskInformation> allDailyTaskInformations() {
|
|
|
+ return dailyTaskInformations;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String start() {
|
|
|
+ String message = "";
|
|
|
+ if (!isStopped()) {
|
|
|
+ message = "已存在运行的定时任务";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(dailyTaskInformations)) {
|
|
|
+ // 线程数与任务数保持一致,这样保证任务间不会互相影响
|
|
|
+ scheduledExecutorService = Executors.newScheduledThreadPool(dailyTaskInformations.size());
|
|
|
+ for (DailyTaskInformation dailyTaskInformation : dailyTaskInformations) {
|
|
|
+ logger.info("New daily task: " + dailyTaskInformation);
|
|
|
+ newDailyTask(dailyTaskInformation.getCommand(), dailyTaskInformation.getHour(),
|
|
|
+ dailyTaskInformation.getMinute(), dailyTaskInformation.getSecond());
|
|
|
+ }
|
|
|
+ message = "已开启定时任务:" + dailyTaskInformations;
|
|
|
+ logger.info(message + "\n");
|
|
|
+ return message;
|
|
|
+ } else {
|
|
|
+ message = "定时任务为空";
|
|
|
+ logger.error(message + "\n");
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String stop() {
|
|
|
+ String message = "";
|
|
|
+ if (isStopped()) {
|
|
|
+ message = "定时任务已经停止或者未开启过";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ logger.info("Remove old daily tasks...");
|
|
|
+ scheduledExecutorService.shutdownNow();
|
|
|
+ message = "已关闭定时任务";
|
|
|
+ logger.info(message + "\n");
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isStopped() {
|
|
|
+ if (scheduledExecutorService == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return scheduledExecutorService.isShutdown() || scheduledExecutorService.isTerminated();
|
|
|
+ }
|
|
|
+
|
|
|
}
|