Quartz と Spring の実装で問題に直面しています。Quartz ジョブ内のコンポーネントを自動配線すると、次のエラーが発生しました。
org.quartz.SchedulerException: Job instantiation failed
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:45) ~[spring-context-support-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127) ~[quartz-2.2.3.jar:na]
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375) [quartz-2.2.3.jar:na]Caused by: java.lang.InstantiationException: com.kaloudia.api.quartz.jobs.ArticlePublicationJob
at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_131]
at org.springframework.scheduling.quartz.AdaptableJobFactory.createJobInstance(AdaptableJobFactory.java:58) ~[spring-context-support-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:41) ~[spring-context-support-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 2 common frames omittedCaused by: java.lang.NoSuchMethodException: com.kaloudia.api.quartz.jobs.ArticlePublicationJob.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_131]
at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_131]
... 4 common frames omitted
BlogManager のインスタンスを取得するために @Autowired アノテーションを使用しますが、コードをデバッグすると、この変数が null であることがわかります。Quartz Job クラス内で BlogManager クラスを取得するにはどうすればよいですか?
私の仕事のクラス:
@Service
public class ArticlePublicationJob implements Job {
private Logger logger = LoggerFactory.getLogger(ArticlePublicationJob.class);
public static final String ARTICLE_ID = "ARTICLE_ID";
private final BlogManager blogManager;
@Autowired
public ArticlePublicationJob(BlogManager blogManager) {
this.blogManager = blogManager;
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
Integer articleId = data.getInt(ARTICLE_ID);
blogManager.actionOnArticle(articleId, "publish", 0);
}
}
と私の Quartz マネージャー:
public class QuartzManager {
private final SchedulerFactoryBean schedulerFactoryBean;
@Inject
public QuartzManager(SchedulerFactoryBean schedulerFactoryBean) {
this.schedulerFactoryBean = schedulerFactoryBean;
}
public void schedulePublishArticleTrigger(Integer articleId, java.util.Calendar publishDate) throws SchedulerException {
Scheduler sched = schedulerFactoryBean.getScheduler();
JobDetail job = JobBuilder.newJob(ArticlePublicationJob.class)
.withIdentity("article_" + articleId, "blog").build();
job.getJobDataMap().put(ArticlePublicationJob.ARTICLE_ID, articleId);
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("PublishArticle_" + articleId, "blog")
.startAt(publishDate.getTime())
.forJob("article_" + articleId, "blog")
.build();
sched.scheduleJob(job, trigger);
}
}