Java Code Examples for com.ruoyi.quartz.domain.SysJob

The following examples show how to use com.ruoyi.quartz.domain.SysJob. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: supplierShop   Source File: SysJobServiceImpl.java    License: MIT License 6 votes vote down vote up
/**
 * 任务调度状态修改
 * 
 * @param job 调度信息
 */
@Override
@Transactional
public int changeStatus(SysJob job) throws SchedulerException
{
    int rows = 0;
    String status = job.getStatus();
    if (ScheduleConstants.Status.NORMAL.getValue().equals(status))
    {
        rows = resumeJob(job);
    }
    else if (ScheduleConstants.Status.PAUSE.getValue().equals(status))
    {
        rows = pauseJob(job);
    }
    return rows;
}
 
Example 2
Source Project: supplierShop   Source File: ScheduleUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 设置定时任务策略
 */
public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb)
        throws TaskException
{
    switch (job.getMisfirePolicy())
    {
        case ScheduleConstants.MISFIRE_DEFAULT:
            return cb;
        case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
            return cb.withMisfireHandlingInstructionIgnoreMisfires();
        case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
            return cb.withMisfireHandlingInstructionFireAndProceed();
        case ScheduleConstants.MISFIRE_DO_NOTHING:
            return cb.withMisfireHandlingInstructionDoNothing();
        default:
            throw new TaskException("The task misfire policy '" + job.getMisfirePolicy()
                    + "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR);
    }
}
 
Example 3
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 6 votes vote down vote up
/**
 * 项目启动时,初始化定时器
 */
@PostConstruct
public void init()
{
    List<SysJob> jobList = jobMapper.selectJobAll();
    for (SysJob job : jobList)
    {
        CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, job.getJobId());
        // 如果不存在,则创建
        if (cronTrigger == null)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        else
        {
            ScheduleUtils.updateScheduleJob(scheduler, job);
        }
    }
}
 
Example 4
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 6 votes vote down vote up
/**
 * 任务调度状态修改
 * 
 * @param job 调度信息
 */
@Override
public int changeStatus(SysJob job)
{
    int rows = 0;
    String status = job.getStatus();
    if (ScheduleConstants.Status.NORMAL.getValue().equals(status))
    {
        rows = resumeJob(job);
    }
    else if (ScheduleConstants.Status.PAUSE.getValue().equals(status))
    {
        rows = pauseJob(job);
    }
    return rows;
}
 
Example 5
Source Project: ruoyiplus   Source File: ScheduleUtils.java    License: MIT License 6 votes vote down vote up
public static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb)
        throws TaskException
{
    switch (job.getMisfirePolicy())
    {
        case ScheduleConstants.MISFIRE_DEFAULT:
            return cb;
        case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
            return cb.withMisfireHandlingInstructionIgnoreMisfires();
        case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
            return cb.withMisfireHandlingInstructionFireAndProceed();
        case ScheduleConstants.MISFIRE_DO_NOTHING:
            return cb.withMisfireHandlingInstructionDoNothing();
        default:
            throw new TaskException("The task misfire policy '" + job.getMisfirePolicy() + "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR);
    }
}
 
Example 6
Source Project: RuoYi   Source File: AbstractQuartzJob.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 执行后
 *
 * @param context        工作执行上下文对象
 */
protected void after(JobExecutionContext context, SysJob sysJob, Exception e) {
    Date startTime = threadLocal.get();
    threadLocal.remove();

    final SysJobLog sysJobLog = new SysJobLog();
    sysJobLog.setJobName(sysJob.getJobName());
    sysJobLog.setJobGroup(sysJob.getJobGroup());
    sysJobLog.setMethodName(sysJob.getMethodName());
    sysJobLog.setMethodParams(sysJob.getMethodParams());
    sysJobLog.setStartTime(startTime);
    sysJobLog.setEndTime(new Date());
    long runMs = sysJobLog.getEndTime().getTime() - sysJobLog.getStartTime().getTime();
    sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
    if (e != null) {
        sysJobLog.setStatus(Constants.FAIL);
        String errorMsg = StrUtil.sub(ExceptionUtil.getExceptionMessage(e), 0, 2000);
        sysJobLog.setExceptionInfo(errorMsg);
    } else {
        sysJobLog.setStatus(Constants.SUCCESS);
    }

    // 写入数据库当中
    SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
}
 
Example 7
Source Project: supplierShop   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
@Log(title = "定时任务", businessType = BusinessType.EXPORT)
@RequiresPermissions("monitor:job:export")
@PostMapping("/export")
@ResponseBody
public AjaxResult export(SysJob job)
{
    List<SysJob> list = jobService.selectJobList(job);
    ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
    return util.exportExcel(list, "定时任务");
}
 
Example 8
Source Project: supplierShop   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 任务调度状态修改
 */
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/changeStatus")
@ResponseBody
public AjaxResult changeStatus(SysJob job) throws SchedulerException
{
    SysJob newJob = jobService.selectJobById(job.getJobId());
    newJob.setStatus(job.getStatus());
    return toAjax(jobService.changeStatus(newJob));
}
 
Example 9
Source Project: supplierShop   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 任务调度立即执行一次
 */
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/run")
@ResponseBody
public AjaxResult run(SysJob job) throws SchedulerException
{
    jobService.run(job);
    return success();
}
 
Example 10
Source Project: RuoYi   Source File: AbstractQuartzJob.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void execute(JobExecutionContext context){
    SysJob sysJob = new SysJob();
    BeanUtil.copyProperties(context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES), sysJob);
    try {
        before(context, sysJob);
        if (ObjectUtil.isNotNull(sysJob)) {
            doExecute(context, sysJob);
        }
        after(context, sysJob, null);
    } catch (Exception e) {
        log.error("任务执行异常  - :", e);
        after(context, sysJob, e);
    }
}
 
Example 11
Source Project: supplierShop   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 修改保存调度
 */
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:edit")
@PostMapping("/edit")
@ResponseBody
public AjaxResult editSave(@Validated SysJob job) throws SchedulerException, TaskException
{
    return toAjax(jobService.updateJob(job));
}
 
Example 12
Source Project: supplierShop   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 校验cron表达式是否有效
 */
@PostMapping("/checkCronExpressionIsValid")
@ResponseBody
public boolean checkCronExpressionIsValid(SysJob job)
{
    return jobService.checkCronExpressionIsValid(job.getCronExpression());
}
 
Example 13
Source Project: supplierShop   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 恢复任务
 * 
 * @param job 调度信息
 */
@Override
@Transactional
public int resumeJob(SysJob job) throws SchedulerException
{
    Long jobId = job.getJobId();
    String jobGroup = job.getJobGroup();
    job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
    int rows = jobMapper.updateJob(job);
    if (rows > 0)
    {
        scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
    }
    return rows;
}
 
Example 14
Source Project: supplierShop   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 删除任务后,所对应的trigger也将被删除
 * 
 * @param job 调度信息
 */
@Override
@Transactional
public int deleteJob(SysJob job) throws SchedulerException
{
    Long jobId = job.getJobId();
    String jobGroup = job.getJobGroup();
    int rows = jobMapper.deleteJobById(jobId);
    if (rows > 0)
    {
        scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
    }
    return rows;
}
 
Example 15
Source Project: supplierShop   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 立即运行任务
 * 
 * @param job 调度信息
 */
@Override
@Transactional
public void run(SysJob job) throws SchedulerException
{
    Long jobId = job.getJobId();
    String jobGroup = job.getJobGroup();
    SysJob properties = selectJobById(job.getJobId());
    // 参数
    JobDataMap dataMap = new JobDataMap();
    dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
    scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, jobGroup), dataMap);
}
 
Example 16
Source Project: supplierShop   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 新增任务
 * 
 * @param job 调度信息 调度信息
 */
@Override
@Transactional
public int insertJob(SysJob job) throws SchedulerException, TaskException
{
    job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
    int rows = jobMapper.insertJob(job);
    if (rows > 0)
    {
        ScheduleUtils.createScheduleJob(scheduler, job);
    }
    return rows;
}
 
Example 17
Source Project: supplierShop   Source File: ScheduleUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 创建定时任务
 */
public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException
{
    Class<? extends Job> jobClass = getQuartzJobClass(job);
    // 构建job信息
    Long jobId = job.getJobId();
    String jobGroup = job.getJobGroup();
    JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build();

    // 表达式调度构建器
    CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
    cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);

    // 按新的cronExpression表达式构建一个新的trigger
    CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup))
            .withSchedule(cronScheduleBuilder).build();

    // 放入参数,运行时的方法可以获取
    jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);

    // 判断是否存在
    if (scheduler.checkExists(getJobKey(jobId, jobGroup)))
    {
        // 防止创建时存在数据问题 先移除,然后在执行创建操作
        scheduler.deleteJob(getJobKey(jobId, jobGroup));
    }

    scheduler.scheduleJob(jobDetail, trigger);

    // 暂停任务
    if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))
    {
        scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
    }
}
 
Example 18
Source Project: RuoYi   Source File: SysJobServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 恢复任务
 *
 * @param job 调度信息
 */
@Override
public int resumeJob(SysJob job) throws SchedulerException{
    job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
    int rows = jobMapper.updateJob(job);
    if (rows > 0) {
        ScheduleUtils.resumeJob(scheduler, job.getJobId());
    }
    return rows;
}
 
Example 19
Source Project: supplierShop   Source File: AbstractQuartzJob.java    License: MIT License 5 votes vote down vote up
/**
 * 执行后
 *
 * @param context 工作执行上下文对象
 * @param sysScheduleJob 系统计划任务
 */
protected void after(JobExecutionContext context, SysJob sysJob, Exception e)
{
    Date startTime = threadLocal.get();
    threadLocal.remove();

    final SysJobLog sysJobLog = new SysJobLog();
    sysJobLog.setJobName(sysJob.getJobName());
    sysJobLog.setJobGroup(sysJob.getJobGroup());
    sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
    sysJobLog.setStartTime(startTime);
    sysJobLog.setEndTime(new Date());
    long runMs = sysJobLog.getEndTime().getTime() - sysJobLog.getStartTime().getTime();
    sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
    if (e != null)
    {
        sysJobLog.setStatus(Constants.FAIL);
        String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000);
        sysJobLog.setExceptionInfo(errorMsg);
    }
    else
    {
        sysJobLog.setStatus(Constants.SUCCESS);
    }

    // 写入数据库当中
    SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
}
 
Example 20
Source Project: ruoyiplus   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 任务调度状态修改
 */
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/changeStatus")
@ResponseBody
public AjaxResult changeStatus(SysJob job)
{
    job.setUpdateBy(ShiroUtils.getLoginName());
    return toAjax(jobService.changeStatus(job));
}
 
Example 21
Source Project: ruoyiplus   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 任务调度立即执行一次
 */
@Log(title = "定时任务", businessType = BusinessType.UPDATE)
@RequiresPermissions("monitor:job:changeStatus")
@PostMapping("/run")
@ResponseBody
public AjaxResult run(SysJob job)
{
    return toAjax(jobService.run(job));
}
 
Example 22
Source Project: ruoyiplus   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 新增保存调度
 */
@Log(title = "定时任务", businessType = BusinessType.INSERT)
@RequiresPermissions("monitor:job:add")
@PostMapping("/add")
@ResponseBody
public AjaxResult addSave(SysJob job)
{
    job.setCreateBy(ShiroUtils.getLoginName());
    return toAjax(jobService.insertJobCron(job));
}
 
Example 23
Source Project: ruoyiplus   Source File: SysJobController.java    License: MIT License 5 votes vote down vote up
/**
 * 校验cron表达式是否有效
 */
@PostMapping("/checkCronExpressionIsValid")
@ResponseBody
public boolean checkCronExpressionIsValid(SysJob job)
{
    return jobService.checkCronExpressionIsValid(job.getCronExpression());
}
 
Example 24
Source Project: RuoYi   Source File: JobInvokeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 执行方法
 *
 * @param sysJob 系统任务
 */
public static void invokeMethod(SysJob sysJob) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException {
    Object bean = SpringUtils.getBean(sysJob.getJobName());
    String methodName = sysJob.getMethodName();
    String methodParams = sysJob.getMethodParams();

    invokeSpringBean(bean, methodName, methodParams);
}
 
Example 25
Source Project: RuoYi   Source File: SysJobServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 更新任务的时间表达式
 *
 * @param job 调度信息
 */
@Override
public int updateJobCron(SysJob job) throws SchedulerException, TaskException{
    int rows = jobMapper.updateJob(job);
    if (rows > 0) {
        updateScheduleJob(scheduler, job);
    }
    return rows;
}
 
Example 26
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 删除任务后,所对应的trigger也将被删除
 * 
 * @param job 调度信息
 */
@Override
public int deleteJob(SysJob job)
{
    int rows = jobMapper.deleteJobById(job.getJobId());
    if (rows > 0)
    {
        ScheduleUtils.deleteScheduleJob(scheduler, job.getJobId());
    }
    return rows;
}
 
Example 27
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 批量删除调度信息
 * 
 * @param ids 需要删除的数据ID
 * @return 结果
 */
@Override
public void deleteJobByIds(String ids)
{
    Long[] jobIds = Convert.toLongArray(ids);
    for (Long jobId : jobIds)
    {
        SysJob job = jobMapper.selectJobById(jobId);
        deleteJob(job);
    }
}
 
Example 28
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 新增任务
 * 
 * @param job 调度信息 调度信息
 */
@Override
public int insertJobCron(SysJob job)
{
    job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
    int rows = jobMapper.insertJob(job);
    if (rows > 0)
    {
        ScheduleUtils.createScheduleJob(scheduler, job);
    }
    return rows;
}
 
Example 29
Source Project: ruoyiplus   Source File: SysJobServiceImpl.java    License: MIT License 5 votes vote down vote up
/**
 * 更新任务的时间表达式
 * 
 * @param job 调度信息
 */
@Override
public int updateJobCron(SysJob job)
{
    int rows = jobMapper.updateJob(job);
    if (rows > 0)
    {
        ScheduleUtils.updateScheduleJob(scheduler, job);
    }
    return rows;
}
 
Example 30
Source Project: RuoYi   Source File: ScheduleUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob job, CronScheduleBuilder cb)
        throws TaskException {
    switch (job.getMisfirePolicy()) {
        case ScheduleConstants.MISFIRE_DEFAULT:
            return cb;
        case ScheduleConstants.MISFIRE_IGNORE_MISFIRES:
            return cb.withMisfireHandlingInstructionIgnoreMisfires();
        case ScheduleConstants.MISFIRE_FIRE_AND_PROCEED:
            return cb.withMisfireHandlingInstructionFireAndProceed();
        case ScheduleConstants.MISFIRE_DO_NOTHING:
            return cb.withMisfireHandlingInstructionDoNothing();
        default:
            throw new TaskException("The task misfire policy '" + job.getMisfirePolicy() + "' cannot be used in cron schedule tasks", Code.CONFIG_ERROR);
    }
}