|
@@ -3,9 +3,11 @@ package cn.com.qmth.examcloud.task.base;
|
|
|
import org.quartz.Job;
|
|
|
import org.quartz.JobExecutionContext;
|
|
|
import org.quartz.JobExecutionException;
|
|
|
+import org.slf4j.MDC;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
|
|
|
+import cn.com.qmth.examcloud.commons.base.logging.SLF4JImpl;
|
|
|
import cn.com.qmth.examcloud.commons.base.util.JsonUtil;
|
|
|
import cn.com.qmth.examcloud.commons.base.util.ThreadLocalUtil;
|
|
|
import cn.com.qmth.examcloud.commons.web.support.SpringContextHolder;
|
|
@@ -23,23 +25,31 @@ public class DistributionJob implements Job {
|
|
|
@Override
|
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
|
String traceId = ThreadLocalUtil.next();
|
|
|
- ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
|
|
|
-
|
|
|
- if (TASK_LOG.isDebugEnabled()) {
|
|
|
- TASK_LOG.debug("distribute job. job detail :" + JsonUtil.toJson(scheduleJob));
|
|
|
+ // 设置MDC
|
|
|
+ if (TASK_LOG instanceof SLF4JImpl) {
|
|
|
+ MDC.put("TRACE_ID", traceId);
|
|
|
}
|
|
|
-
|
|
|
+ ScheduleJob scheduleJob = null;
|
|
|
try {
|
|
|
+ scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
|
|
|
+
|
|
|
+ if (TASK_LOG.isDebugEnabled()) {
|
|
|
+ TASK_LOG.debug("distribute job. job detail :" + JsonUtil.toJson(scheduleJob));
|
|
|
+ }
|
|
|
Object bean = SpringContextHolder.getBean(scheduleJob.getSpringBean());
|
|
|
|
|
|
Task task = (Task) bean;
|
|
|
task.execute(scheduleJob, traceId);
|
|
|
} catch (Exception e) {
|
|
|
if (TASK_LOG.isErrorEnabled()) {
|
|
|
- TASK_LOG.error(
|
|
|
- "fail to distribute job. job detail :" + JsonUtil.toJson(scheduleJob), e);
|
|
|
+ TASK_LOG.error("fail to distribute job. job detail :" + JsonUtil.toJson(scheduleJob), e);
|
|
|
}
|
|
|
throw new JobExecutionException(e);
|
|
|
+ } finally {
|
|
|
+ // 清理MDC
|
|
|
+ if (TASK_LOG instanceof SLF4JImpl) {
|
|
|
+ MDC.clear();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|