这是我用于将新的cron作业保存到postgresql数据库的代码。但它显示了一些编译时异常。我最初使用spring boot web app将quartz 1.8.6迁移到2.2.1。查看附件
private static final String JOBNAME = "sampleJobRunner";
private static final String JOB_GROUPNAME = "sampleGroup";
public void saveScheduledTime(String name) throws SchedulerException, ParseException {
Scheduler scheduler;
CronTrigger trigger;
String cronExp = null;
scheduler = this.quartzScheduler.getObject();
trigger = (CronTrigger) scheduler.getTrigger(name, JOB_GROUPNAME);
if (scheduler != null) {
cronExp = "0 0 12 ? 1 MON#1 *";
if (trigger != null) {
rescheduleJob(scheduler, trigger.getName(), cronExp);
} else {
createNewCronTrigger(scheduler, cronExp, name);
}
}
}
private void rescheduleJob(Scheduler scheduler, String triggerName, String cronExp)
throws SchedulerException, ParseException {
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName, JOB_GROUPNAME);
trigger.setCronExpression(cronExp);
scheduler.rescheduleJob(triggerName, JOB_GROUPNAME, trigger);
}
private void createNewCronTrigger(Scheduler scheduler, String cronExp, String sentType)
throws SchedulerException, ParseException {
CronTrigger trigger = new CronTrigger(sentType, JOB_GROUPNAME, JOBNAME, JOB_GROUPNAME, cronExp);
scheduler.scheduleJob(trigger);
scheduler.start();
}
Spring boot quartz configuration
@Configuration
@ConditionalOnProperty(name = "quartz.enabled")
public class SchedulerConfig {
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JobFactory jobFactory,
@Qualifier("sampleJobTrigger") Trigger sampleJobTrigger) throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// this allows to update triggers in DB when updating settings in config
// file:
factory.setOverwriteExistingJobs(true);
factory.setDataSource(dataSource);
factory.setJobFactory(jobFactory);
factory.setQuartzProperties(quartzProperties());
// factory.setTriggers(sampleJobTrigger);
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
@Bean
public JobDetailFactoryBean sampleJobDetail() {
return createJobDetail(SampleJob.class);
}
@Bean(name = "sampleJobTrigger")
public SimpleTriggerFactoryBean sampleJobTrigger(@Qualifier("sampleJobDetail") JobDetail jobDetail,
@Value("${samplejob.frequency}") long frequency) {
return createTrigger(jobDetail, frequency);
}
private static JobDetailFactoryBean createJobDetail(Class jobClass) {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
factoryBean.setJobClass(jobClass);
// job has to be durable to be stored in DB:
factoryBean.setDurability(true);
return factoryBean;
}
private static SimpleTriggerFactoryBean createTrigger(JobDetail jobDetail, long pollFrequencyMs) {
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(0L);
factoryBean.setRepeatInterval(pollFrequencyMs);
factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
// in case of misfire, ignore all missed triggers and continue :
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
return factoryBean;
}
}
答案 0 :(得分:1)
修复代码中的complilation错误:
在github上查看我的Spring Boot + Quartz示例项目:https://github.com/davidkiss/spring-boot-quartz-demo。我用一个新的createCronTrigger帮助器方法更新了SchedulerConfig类,该方法允许在Spring Boot中创建cron触发器:
private static CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setCronExpression(cronExpression);
factoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
return factoryBean;
}
如果在项目中使用Spring Boot,我相信使用SchedulerConfig类是一种更简洁的方法,可以在Spring Boot中配置quartz作业,然后调用saveScheduledTime()方法。
为了创建任何cron触发器,我将在SchedulerConfig类中添加其他方法来创建类似于sampleJobTrigger的相应触发器bean。
答案 1 :(得分:0)
这纯粹是从Quartz 1.8.6迁移到2.2.1的结果。 Quartz使用2.x版本进行了一些代码更改。您需要在应用程序中进行更改以尊重这一点。看看这个:http://www.quartz-scheduler.org/documentation/quartz-2.x/migration-guide.html