实例化作业 Quartz 时出现异常
Exception while instantiating job Quartz
我正在尝试将 Quartz 与 spring 引导一起使用,但出现此异常:
2021-04-01 19:07:25.753 错误 13088 --- [SchedulerThread] org.quartz.core.ErrorLogger:实例化要执行的作业时出错。工作= 'ecadb474-77b7-45bd-926a-caee31728465'
org.quartz.SchedulerException: 实例化问题 class 'ClassJob' -
在 org.quartz.core.JobRunShell.initialize(JobRunShell.java:135) ~[quartz-2.2.0.jar:na]
在 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:378) [quartz-2.2.0.jar:na]
原因:java.lang.AbstractMethodError:config.AutowiringSpringBeanJobFactory.newJob(Lorg/quartz/spi/TriggerFiredBundle;Lorg/quartz/Scheduler;)Lorg/quartz/Job;
在 org.quartz.core.JobRunShell.initialize(JobRunShell.java:128) ~[quartz-2.2.0.jar:na]
... 省略了 1 个公共框架
这是我的代码:
班级工作:
@Component
public class ClassJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory
.getLogger(ClassJob.class);
@Override
public void executeInternal(JobExecutionContext jobExecutionContext)
throws JobExecutionException {
logger
.info(
"Executing Job with key {}",
jobExecutionContext.getJobDetail().getKey());
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
int controlRuleNum = jobDataMap.getInt("controlRuleNum");
executeRule(controlRuleNum);
}
private void executeRule(int ebControlRuleNum) {
System.out.println(ebControlRuleNum);
}
}
配置:
@Configuration
public class QuartzSchedulerConfiguration {
@Autowired
ApplicationContext applicationContext;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@PostConstruct
public void init() {
logger.debug("QuartzConfig initialized.");
}
@Bean
public SchedulerFactoryBean scheduler() {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
return quartzScheduler;
}
@Bean
public JobFactory jobFactory() {
return new AutowiringSpringBeanJobFactory();
}
}
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory
implements ApplicationContextAware {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
autowireCapableBeanFactory = applicationContext
.getAutowireCapableBeanFactory();
}
@Override
public Object createJobInstance(final TriggerFiredBundle bundle) {
final Object job = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(job);
autowireCapableBeanFactory
.initializeBean(job, job.getClass().getName());
return job;
}
}
private void scheduleJob(int ebControlRuleNum, Date date) {
try {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("controlRuleNum", ebControlRuleNum);
JobDetail jobDetail = JobBuilder
.newJob(ClassJob.class)
.withIdentity(UUID.randomUUID().toString(), "controlRule-jobs")
.withDescription("call control rule Job")
.usingJobData(jobDataMap).storeDurably().build();
Trigger trigger = TriggerBuilder
.newTrigger().forJob(jobDetail)
.withIdentity(
jobDetail.getKey().getName(),
"controlRule-triggers")
.withDescription("controlRule Trigger").startAt(date)
.build();
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException ex) {
logger.error("Error scheduling email", ex);
}
}
谁能指出我哪里错了?
为了将来的参考,我添加了“spring-context-support”依赖项并且它有效
我正在尝试将 Quartz 与 spring 引导一起使用,但出现此异常:
2021-04-01 19:07:25.753 错误 13088 --- [SchedulerThread] org.quartz.core.ErrorLogger:实例化要执行的作业时出错。工作= 'ecadb474-77b7-45bd-926a-caee31728465'
org.quartz.SchedulerException: 实例化问题 class 'ClassJob' - 在 org.quartz.core.JobRunShell.initialize(JobRunShell.java:135) ~[quartz-2.2.0.jar:na] 在 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:378) [quartz-2.2.0.jar:na] 原因:java.lang.AbstractMethodError:config.AutowiringSpringBeanJobFactory.newJob(Lorg/quartz/spi/TriggerFiredBundle;Lorg/quartz/Scheduler;)Lorg/quartz/Job; 在 org.quartz.core.JobRunShell.initialize(JobRunShell.java:128) ~[quartz-2.2.0.jar:na] ... 省略了 1 个公共框架
这是我的代码: 班级工作:
@Component
public class ClassJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory
.getLogger(ClassJob.class);
@Override
public void executeInternal(JobExecutionContext jobExecutionContext)
throws JobExecutionException {
logger
.info(
"Executing Job with key {}",
jobExecutionContext.getJobDetail().getKey());
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
int controlRuleNum = jobDataMap.getInt("controlRuleNum");
executeRule(controlRuleNum);
}
private void executeRule(int ebControlRuleNum) {
System.out.println(ebControlRuleNum);
}
}
配置:
@Configuration
public class QuartzSchedulerConfiguration {
@Autowired
ApplicationContext applicationContext;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@PostConstruct
public void init() {
logger.debug("QuartzConfig initialized.");
}
@Bean
public SchedulerFactoryBean scheduler() {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
return quartzScheduler;
}
@Bean
public JobFactory jobFactory() {
return new AutowiringSpringBeanJobFactory();
}
}
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory
implements ApplicationContextAware {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
autowireCapableBeanFactory = applicationContext
.getAutowireCapableBeanFactory();
}
@Override
public Object createJobInstance(final TriggerFiredBundle bundle) {
final Object job = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(job);
autowireCapableBeanFactory
.initializeBean(job, job.getClass().getName());
return job;
}
}
private void scheduleJob(int ebControlRuleNum, Date date) {
try {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("controlRuleNum", ebControlRuleNum);
JobDetail jobDetail = JobBuilder
.newJob(ClassJob.class)
.withIdentity(UUID.randomUUID().toString(), "controlRule-jobs")
.withDescription("call control rule Job")
.usingJobData(jobDataMap).storeDurably().build();
Trigger trigger = TriggerBuilder
.newTrigger().forJob(jobDetail)
.withIdentity(
jobDetail.getKey().getName(),
"controlRule-triggers")
.withDescription("controlRule Trigger").startAt(date)
.build();
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException ex) {
logger.error("Error scheduling email", ex);
}
}
谁能指出我哪里错了?
为了将来的参考,我添加了“spring-context-support”依赖项并且它有效