Search in sources :

Example 1 with SysJobLogService

use of com.jun.plugin.system.service.SysJobLogService in project jun_springboot_api_service by wujun728.

the class ScheduleJob method executeInternal.

@Override
protected void executeInternal(JobExecutionContext context) {
    SysJobEntity scheduleJob = (SysJobEntity) context.getMergedJobDataMap().get(SysJobEntity.JOB_PARAM_KEY);
    // 获取spring bean
    SysJobLogService scheduleJobLogService = (SysJobLogService) SpringContextUtils.getBean("sysJobLogService");
    // 数据库保存执行记录
    SysJobLogEntity log = new SysJobLogEntity();
    log.setJobId(scheduleJob.getId());
    log.setBeanName(scheduleJob.getBeanName());
    log.setParams(scheduleJob.getParams());
    // 任务开始时间
    long startTime = System.currentTimeMillis();
    try {
        // 执行任务
        logger.debug("任务准备执行,任务ID:" + scheduleJob.getId());
        Object target = SpringContextUtils.getBean(scheduleJob.getBeanName());
        assert target != null;
        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.getId() + "  总共耗时:" + times + "毫秒");
    } catch (Exception e) {
        logger.error("任务执行失败,任务ID:" + scheduleJob.getId(), e);
        // 任务执行总时长
        long times = System.currentTimeMillis() - startTime;
        log.setTimes((int) times);
        // 任务状态    0:成功    1:失败
        log.setStatus(1);
        log.setError(StringUtils.substring(e.toString(), 0, 2000));
    } finally {
        assert scheduleJobLogService != null;
        scheduleJobLogService.save(log);
    }
}
Also used : SysJobLogService(com.jun.plugin.system.service.SysJobLogService) SysJobLogEntity(com.jun.plugin.system.entity.SysJobLogEntity) Method(java.lang.reflect.Method) SysJobEntity(com.jun.plugin.system.entity.SysJobEntity)

Aggregations

SysJobEntity (com.jun.plugin.system.entity.SysJobEntity)1 SysJobLogEntity (com.jun.plugin.system.entity.SysJobLogEntity)1 SysJobLogService (com.jun.plugin.system.service.SysJobLogService)1 Method (java.lang.reflect.Method)1