簡體   English   中英

無法安排Quartz作業

[英]Unable to schedule Quartz job

我用Quartz寫了一個小的Spring Boot服務。 我正在嘗試使用下面的代碼安排工作。 但是,我反復收到以下錯誤:

ERROR[my-service] [2017-01-22 17:38:37,328] [] [main] [SpringApplication]: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerFactoryBean' defined in class path resource [com/myservice/configuration/QuartzConfiguration.class]: Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: Couldn't store trigger 'group1.trigger1' for 'group1.job1' job:The job (group1.job1) referenced by the trigger does not exist. [See nested exception: org.quartz.JobPersistenceException: The job (group1.job1) referenced by the trigger does not exist.]

毋庸置疑,這項工作永遠不會安排好。 這是相關代碼:

@Slf4j
@NoArgsConstructor
public class TimedJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.debug("**********timed job****************");
    }
}

@Configuration
public class QuartzConfiguration {
...
    @Inject
    MyDao myDao;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(ApplicationContext applicationContext) throws SchedulerException {

        SchedulerFactoryBean factory = new SchedulerFactoryBean();

        factory.setAutoStartup(true);
        factory.setOverwriteExistingJobs(true);

        QuartzJobFactory jobFactory = new QuartzJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        factory.setJobFactory(jobFactory);

        factory.setDataSource(dataSource());
        factory.setSchedulerContextAsMap(Collections.singletonMap("my_dao", myDao));

        JobDetail jobDetail = JobBuilder.newJob()
                .ofType(TimedJob.class)
                .storeDurably()
                .withDescription("desc")
                .withIdentity("job1", "group1")
                .build();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 10);
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .startAt(calendar.getTime())
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()
                )
                .forJob(jobDetail)
                .withIdentity("trigger1", "group1")
                .build();
        factory.setTriggers(trigger);

        Properties quartzProperties = new Properties();
        quartzProperties.setProperty("org.quartz.scheduler.instanceName", instanceName);
        quartzProperties.setProperty("org.quartz.scheduler.instanceId", instanceId);
        quartzProperties.setProperty("org.quartz.threadPool.threadCount", threadCount);
        quartzProperties.setProperty("org.quartz.jobStore.driverDelegateClass", driverDelegateClassName);
        factory.setQuartzProperties(quartzProperties);

        factory.start();

        return factory;
    }

這幾乎完全從石英教程復制在這里 我究竟做錯了什么?

我想你需要將你的jobDetail設置為你的工廠實例。

以下網址可能對您有所幫助。 http://www.baeldung.com/spring-quartz-schedule

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM