简体   繁体   English

无法安排Quartz作业

[英]Unable to schedule Quartz job

I have written a small Spring Boot service using Quartz . 我用Quartz写了一个小的Spring Boot服务。 I am trying to schedule a job using the code below. 我正在尝试使用下面的代码安排工作。 However, I am repeatedly getting the following error: 但是,我反复收到以下错误:

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.]

Needless to say, the job never gets scheduled. 毋庸置疑,这项工作永远不会安排好。 Here is the relevant code: 这是相关代码:

@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;
    }

This is almost exactly copied from the Quartz tutorials here . 这几乎完全从石英教程复制在这里 What am I doing wrong? 我究竟做错了什么?

I guess you need setting your jobDetail to your factory instance. 我想你需要将你的jobDetail设置为你的工厂实例。

The below URL might help you. 以下网址可能对您有所帮助。 http://www.baeldung.com/spring-quartz-schedule http://www.baeldung.com/spring-quartz-schedule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM