Search in sources :

Example 1 with QuartzJob

use of com.dimple.project.tool.domain.QuartzJob in project DimpleBlog by martin-chips.

the class QuartzJobServiceImpl method executeQuartzJobById.

@Override
public void executeQuartzJobById(Long id) {
    QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
    if (Objects.isNull(quartzJob)) {
        throw new CustomException("当前任务不存在");
    }
    quartzManage.runAJobNow(quartzJob);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Example 2 with QuartzJob

use of com.dimple.project.tool.domain.QuartzJob in project DimpleBlog by martin-chips.

the class ExecutionJob method executeInternal.

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
    // 获取spring bean
    QuartzJobService quartzJobService = SpringUtils.getBean(QuartzJobService.class);
    QuartzJobLogService quartzJobLogService = SpringUtils.getBean(QuartzJobLogService.class);
    QuartzJobLog quartzJobLog = new QuartzJobLog();
    quartzJobLog.setJobName(quartzJob.getJobName());
    quartzJobLog.setBeanName(quartzJob.getBeanName());
    quartzJobLog.setMethodName(quartzJob.getMethodName());
    quartzJobLog.setMethodParams(quartzJob.getMethodParams());
    quartzJobLog.setCronExpression(quartzJob.getCronExpression());
    long startTime = System.currentTimeMillis();
    try {
        // 执行任务
        log.info("任务准备执行,任务名称:{}", quartzJob.getJobName());
        QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(), quartzJob.getMethodParams());
        Future<Object> future = threadPoolTaskExecutor.submit(task);
        Object result = future.get();
        log.info("任务返回值:{}", result);
        quartzJobLog.setResult(result.toString());
        long times = System.currentTimeMillis() - startTime;
        quartzJobLog.setCost(times);
        // 任务状态
        quartzJobLog.setStatus(true);
        log.info("任务执行完毕,任务名称:{} 总共耗时:{} 毫秒", quartzJob.getJobName(), times);
    } catch (Exception e) {
        log.error("任务执行失败,任务名称:{}" + quartzJob.getJobName(), e);
        long times = System.currentTimeMillis() - startTime;
        quartzJobLog.setCost(times);
        quartzJobLog.setStatus(false);
        quartzJobLog.setException(getStackTrace(e));
        // 设置为暂停状态
        quartzJob.setStatus(false);
        // 更新状态
        quartzJobService.updateQuartzJob(quartzJob);
    } finally {
        quartzJobLogService.insertQuartzJobLog(quartzJobLog);
    }
}
Also used : QuartzJobService(com.dimple.project.tool.service.QuartzJobService) QuartzJob(com.dimple.project.tool.domain.QuartzJob) QuartzJobLogService(com.dimple.project.log.service.QuartzJobLogService) QuartzJobLog(com.dimple.project.log.domain.QuartzJobLog) JobExecutionException(org.quartz.JobExecutionException)

Example 3 with QuartzJob

use of com.dimple.project.tool.domain.QuartzJob in project DimpleBlog by martin-chips.

the class QuartzJobServiceImpl method deleteQuartzJob.

@Override
public int deleteQuartzJob(Long id) {
    QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
    if (Objects.isNull(quartzJob)) {
        throw new CustomException("当前任务不存在");
    }
    quartzManage.deleteJob(quartzJob);
    String username = SecurityUtils.getUsername();
    return quartzJobMapper.deleteQuartzJobById(id, username);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Example 4 with QuartzJob

use of com.dimple.project.tool.domain.QuartzJob in project DimpleBlog by martin-chips.

the class QuartzJobServiceImpl method updateQuartzJobStatus.

@Override
public int updateQuartzJobStatus(Long id) {
    QuartzJob quartzJob = quartzJobMapper.selectQuartzJobById(id);
    if (Objects.isNull(quartzJob)) {
        throw new CustomException("当前任务不存在");
    }
    // 如果当前为运行状态
    if (quartzJob.getStatus().booleanValue()) {
        quartzManage.pauseJob(quartzJob);
    } else {
        quartzManage.resumeJob(quartzJob);
    }
    quartzJob.setStatus(!quartzJob.getStatus());
    return quartzJobMapper.updateQuartzJob(quartzJob);
}
Also used : QuartzJob(com.dimple.project.tool.domain.QuartzJob) CustomException(com.dimple.common.exception.CustomException)

Aggregations

QuartzJob (com.dimple.project.tool.domain.QuartzJob)4 CustomException (com.dimple.common.exception.CustomException)3 QuartzJobLog (com.dimple.project.log.domain.QuartzJobLog)1 QuartzJobLogService (com.dimple.project.log.service.QuartzJobLogService)1 QuartzJobService (com.dimple.project.tool.service.QuartzJobService)1 JobExecutionException (org.quartz.JobExecutionException)1