Browse Source

Merge remote-tracking branch 'origin/master'

master
Rui_Li 4 years ago
parent
commit
3b90214af6
  1. 2
      src/main/java/com/gaotao/modules/job/entity/ScheduleJobLogEntity.java
  2. 3
      src/main/java/com/gaotao/modules/job/task/ITask.java
  3. 5
      src/main/java/com/gaotao/modules/job/task/TestTask.java
  4. 59
      src/main/java/com/gaotao/modules/job/utils/ScheduleJob.java

2
src/main/java/com/gaotao/modules/job/entity/ScheduleJobLogEntity.java

@ -62,4 +62,6 @@ public class ScheduleJobLogEntity implements Serializable {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime; private Date createTime;
private String errorMsg;
} }

3
src/main/java/com/gaotao/modules/job/task/ITask.java

@ -14,5 +14,6 @@ public interface ITask {
* *
* @param params 参数多参数使用JSON数据 * @param params 参数多参数使用JSON数据
*/ */
void run(String params);
String run(String params);
} }

5
src/main/java/com/gaotao/modules/job/task/TestTask.java

@ -17,9 +17,10 @@ import org.springframework.stereotype.Component;
public class TestTask implements ITask { public class TestTask implements ITask {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void run(String params){
public String run(String params){
logger.debug("TestTask定时任务正在执行,参数为:{}", params); logger.debug("TestTask定时任务正在执行,参数为:{}", params);
return "执行结果";
} }
} }

59
src/main/java/com/gaotao/modules/job/utils/ScheduleJob.java

@ -15,20 +15,19 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 定时任务 * 定时任务
*
*
*/ */
public class ScheduleJob extends QuartzJobBean { public class ScheduleJob extends QuartzJobBean {
private Logger logger = LoggerFactory.getLogger(getClass());
private Logger logger = LoggerFactory.getLogger(getClass());
@Override @Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap() ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap()
.get(ScheduleJobEntity.JOB_PARAM_KEY);
.get(ScheduleJobEntity.JOB_PARAM_KEY);
//获取spring bean //获取spring bean
ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils.getBean("scheduleJobLogService"); ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils.getBean("scheduleJobLogService");
@ -45,31 +44,31 @@ public class ScheduleJob extends QuartzJobBean {
try { try {
//执行任务 //执行任务
logger.debug("任务准备执行,任务ID:" + scheduleJob.getJobId());
Object target = SpringContextUtils.getBean(scheduleJob.getBeanName());
Method method = target.getClass().getDeclaredMethod("run", String.class);
method.invoke(target, scheduleJob.getParams());
//任务执行总时长
long times = System.currentTimeMillis() - startTime;
log.setTimes((int)times);
//任务状态 0成功 1失败
log.setStatus(0);
logger.debug("任务执行完毕,任务ID:" + scheduleJob.getJobId() + " 总共耗时:" + times + "毫秒");
} catch (Exception e) {
logger.error("任务执行失败,任务ID:" + scheduleJob.getJobId(), e);
//任务执行总时长
long times = System.currentTimeMillis() - startTime;
log.setTimes((int)times);
//任务状态 0成功 1失败
log.setStatus(1);
log.setError(StringUtils.substring(e.toString(), 0, 2000));
}finally {
scheduleJobLogService.save(log);
}
logger.debug("任务准备执行,任务ID:" + scheduleJob.getJobId());
Object target = SpringContextUtils.getBean(scheduleJob.getBeanName());
Method method = target.getClass().getDeclaredMethod("run", String.class);
Object invoke = method.invoke(target, scheduleJob.getParams());
//任务执行总时长
long times = System.currentTimeMillis() - startTime;
log.setTimes((int) times);
//任务状态 0成功 1失败
log.setStatus(0);
log.setErrorMsg(invoke.toString());
logger.debug("任务执行完毕,任务ID:" + scheduleJob.getJobId() + " 总共耗时:" + times + "毫秒");
} catch (Exception e) {
logger.error("任务执行失败,任务ID:" + scheduleJob.getJobId(), e);
//任务执行总时长
long times = System.currentTimeMillis() - startTime;
log.setTimes((int) times);
//任务状态 0成功 1失败
log.setStatus(1);
log.setErrorMsg(e.getMessage());
log.setError(StringUtils.substring(e.toString(), 0, 2000));
} finally {
scheduleJobLogService.save(log);
}
} }
} }
Loading…
Cancel
Save