Search in sources :

Example 1 with SysJobLogEntity

use of com.company.project.entity.SysJobLogEntity in project springboot-manager by aitangbao.

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.company.project.service.SysJobLogService) SysJobLogEntity(com.company.project.entity.SysJobLogEntity) Method(java.lang.reflect.Method) SysJobEntity(com.company.project.entity.SysJobEntity)

Example 2 with SysJobLogEntity

use of com.company.project.entity.SysJobLogEntity in project springboot-manager by aitangbao.

the class SysJobLogController method findListByPage.

@ApiOperation(value = "查询分页数据")
@PostMapping("/listByPage")
@RequiresPermissions("sysJob:list")
public DataResult findListByPage(@RequestBody SysJobLogEntity sysJobLog) {
    Page page = new Page(sysJobLog.getPage(), sysJobLog.getLimit());
    LambdaQueryWrapper<SysJobLogEntity> queryWrapper = Wrappers.lambdaQuery();
    // 查询条件示例
    if (!StringUtils.isEmpty(sysJobLog.getJobId())) {
        queryWrapper.like(SysJobLogEntity::getJobId, sysJobLog.getJobId());
    }
    queryWrapper.orderByDesc(SysJobLogEntity::getCreateTime);
    IPage<SysJobLogEntity> iPage = sysJobLogService.page(page, queryWrapper);
    return DataResult.success(iPage);
}
Also used : Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) SysJobLogEntity(com.company.project.entity.SysJobLogEntity) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SysJobLogEntity (com.company.project.entity.SysJobLogEntity)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 SysJobEntity (com.company.project.entity.SysJobEntity)1 SysJobLogService (com.company.project.service.SysJobLogService)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Method (java.lang.reflect.Method)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1