无法安排Quartz作业

时间:2017-01-22 16:37:01

标签: spring spring-boot quartz-scheduler

我使用Spring Boot编写了一个小Quartz服务。我正在尝试使用下面的代码安排工作。但是,我反复收到以下错误:

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

这几乎完全是从Quartz教程here中复制而来的。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我猜您需要将jobDetail设置为工厂实例。

以下网址可能会对您有所帮助。 http://www.baeldung.com/spring-quartz-schedule