如何修复无法正常工作的 Spring Boot Quartz Cron Trigger Job

问题描述

创建了一个简单的 spring boot 应用程序源。应用程序的意图是动态创建作业并使用触发器触发它。

我正在尝试使用它不起作用的 cron 表达式。

但是简单的触发器在 30 秒后运行,这是有效的。

任何人都可以在这里帮助我解决应用程序源的问题。在 GitHub 公共存储库上共享了一份副本。您可以通过点击 here 进行克隆。

在随附来源的自述文件中重现问题的步骤。

application.properties

spring.quartz.job-store-type=memory
spring.quartz.properties.org.quartz.threadPool.threadCount=5

# logging level
#logging.level.org.springframework=ERROR
logging.level.com.example=DEBUG
logging.level.org.quartz=DEBUG

# output to a file
logging.file=app.log

# temp folder example
#logging.file=${java.io.tmpdir}/app.log

logging.pattern.file=%d %p %c{1.} [%t] %m%n

logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n

休息控制器

package com.example.QuartzREST;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api",produces = "application/hal+json")
public class QuartzRestResource {

    @Autowired private QartzRestService qartzRestService;

    @SneakyThrows
    @GetMapping("/schedule/cron/job/{id}")
    public String scheduleCronJob(@PathVariable("id") String id) {
        qartzRestService.scheduleJobWithcrontrigger(id,"0/5 0 0 ? * * *");
        return "OK";
    }

    @SneakyThrows
    @GetMapping("/schedule/simple/job/{id}")
    public String scheduleSimpleJob(@PathVariable("id") String id) {
        qartzRestService.scheduleJobWithSimpleTrigger(id);
        return "OK";
    }
}

QartzRestService

package com.example.QuartzREST;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

/**
 * @author  Ameya Shetti
 */
@Slf4j
@Service
public class QartzRestService {

    @Autowired private Scheduler scheduler;

    public void scheduleJobWithcrontrigger(String jobId,String cronExpresson) throws SchedulerException {
        JobDetail jobDetail = this.buildJobDetail(jobId);
        scheduler.scheduleJob(jobDetail,buildJobcrontrigger(jobId,jobDetail,cronExpresson));
        scheduler.start();
    }

    public void scheduleJobWithSimpleTrigger(String jobId) throws SchedulerException {
        JobDetail jobDetail = this.buildJobDetail(jobId);
        scheduler.scheduleJob(jobDetail,buildJobSimpleTrigger(jobId,jobDetail));
        scheduler.start();
    }

    private JobDetail buildJobDetail(String jobId) {
        JobDataMap jobDataMap = new JobDataMap();

        jobDataMap.put(QartzRestConstants.KEY_JOB_ID,jobId);

        return JobBuilder.newJob(QartzRestJob.class)
                .withIdentity(QartzRestConstants.BATCHJOB+jobId,QartzRestConstants.GROUP1)
                .usingJobData(jobDataMap)
                .storeDurably()
                .build();
    }

    private Trigger buildJobSimpleTrigger(String jobId,JobDetail jobDetail) {

        return TriggerBuilder.newTrigger()
                .withIdentity(QartzRestConstants.BATCH_TRIGGER+jobId,QartzRestConstants.GROUP1)
                .forJob(jobDetail)
                .startAt(Date.from(Instant.Now().plusSeconds(30)))
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow())
                .build();
    }

    private Trigger buildJobcrontrigger(String jobId,JobDetail jobDetail,String cronExpression) {
        log.debug("cron expression used : {}",cronExpression);

        return TriggerBuilder.newTrigger()
                .withIdentity(QartzRestConstants.BATCH_TRIGGER+jobId,QartzRestConstants.GROUP1)
                .forJob(jobDetail)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                .build();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)