wangwei 7 éve
szülő
commit
62bfd5dfc8

+ 43 - 0
examcloud-task-base/src/main/java/cn/com/qmth/examcloud/task/base/TaskTracker.java

@@ -0,0 +1,43 @@
+package cn.com.qmth.examcloud.task.base;
+
+import org.quartz.JobExecutionContext;
+
+/**
+ * 任务跟踪记载器
+ *
+ * @author WANGWEI
+ * @date 2018年7月25日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public interface TaskTracker {
+
+	/**
+	 * 启动
+	 *
+	 * @author WANGWEI
+	 * @param scheduleJob
+	 * @param traceId
+	 */
+	void start(JobExecutionContext context, final ScheduleJob scheduleJob, final String traceId);
+
+	/**
+	 * 异常
+	 *
+	 * @author WANGWEI
+	 * @param scheduleJob
+	 * @param traceId
+	 * @param e
+	 */
+	void whenException(JobExecutionContext context, final ScheduleJob scheduleJob,
+			final String traceId, Exception e);
+
+	/**
+	 * 结束
+	 *
+	 * @author WANGWEI
+	 * @param scheduleJob
+	 * @param traceId
+	 */
+	void onEnd(JobExecutionContext context, final ScheduleJob scheduleJob, final String traceId);
+
+}

+ 61 - 0
examcloud-task-service/src/main/java/cn/com/qmth/examcloud/task/service/DefaultTaskTracker.java

@@ -0,0 +1,61 @@
+package cn.com.qmth.examcloud.task.service;
+
+import java.util.Date;
+
+import javax.transaction.Transactional;
+
+import org.quartz.JobExecutionContext;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import cn.com.qmth.examcloud.task.base.ScheduleJob;
+import cn.com.qmth.examcloud.task.base.TaskTracker;
+import cn.com.qmth.examcloud.task.dao.TaskTraceRepo;
+import cn.com.qmth.examcloud.task.dao.entity.TaskTraceEntity;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年7月25日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@Component
+public class DefaultTaskTracker implements TaskTracker {
+
+	@Autowired
+	TaskTraceRepo taskTraceRepo;
+
+	@Transactional
+	@Override
+	public void start(JobExecutionContext context, ScheduleJob scheduleJob, String traceId) {
+		TaskTraceEntity entity = new TaskTraceEntity();
+		entity.setJobId(scheduleJob.getJobId());
+		entity.setStartTime(new Date());
+		entity.setTraceId(traceId);
+		entity.setJobStatus("启动");
+		taskTraceRepo.save(entity);
+	}
+
+	@Transactional
+	@Override
+	public void whenException(JobExecutionContext context, ScheduleJob scheduleJob, String traceId,
+			Exception e) {
+		TaskTraceEntity entity = taskTraceRepo.findByTraceId(traceId);
+		entity.setJobStatus("异常");
+		entity.setEndTime(new Date());
+		entity.setException(e.getCause().getMessage());
+		taskTraceRepo.save(entity);
+	}
+
+	@Transactional
+	@Override
+	public void onEnd(JobExecutionContext context, ScheduleJob scheduleJob, String traceId) {
+		TaskTraceEntity entity = taskTraceRepo.findByTraceId(traceId);
+		entity.setJobStatus("成功");
+		entity.setEndTime(new Date());
+		taskTraceRepo.save(entity);
+
+	}
+
+}