2017-01-22 84 views
0

我写了一个小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复制。我究竟做错了什么?

+0

尝试使用'@EnableScheduling'在方法运行时自动配置和使用'@Scheduled'注释作为替代方法 – rajadilipkolli

回答