简体   繁体   English

Quartz和Spring Boot的问题

[英]Issue with Quartz and Spring Boot

So I have a technical challenge I need help with. 因此,我遇到了技术难题,需要帮助。

A large scale project is using a Quartz scheduler to schedule a job to run every night at 9. 一个大型项目正在使用Quartz调度程序来调度一项工作,该工作每天晚上9点运行。
The Job that is scheduled, however needs to read values from property files, get some beans using auto-wiring etc. 计划的作业,但是需要从属性文件中读取值,使用自动装配等获取一些bean。

When I used @Autowired and @Value annotations, I found the values to be null. 当我使用@Autowired和@Value批注时,我发现这些值为null。

The issue is that Quartz creates JobDetail objects using newJob() outside the spring container. 问题是Quartz使用spring容器外的newJob()创建JobDetail对象。 As can be seen in the below code. 如下面的代码所示。

JobKey jobKey = new JobKey("NightJob", "9-PM Job");
JobDetail jobDetail = newJob(NightJob.class).withIdentity(jobKey)
                     .usingJobData("Job-Id", "1")
                     .build();

The jobDetail object which wraps NightJob thus cannot access property files or beans using spring. 因此,包装NightJobjobDetail对象无法使用spring访问属性文件或bean。

Here is my NightJob class 这是我的NightJob课程

public class NightJob implements Job{

    //@Value to read from property file; here
    //@Autowired to use some beans; here

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException{
    }
}

I scanned Stack Overflow and shortlisted several solutions. 我扫描了堆栈溢出并列出了几种解决方案。 I also read through the comments and listed the top counter-comments. 我还阅读了这些评论,并列出了最重要的反评论。

Suggestion 1: Get rid of Quartz and use Spring Batch due to its good integration with Spring Boot 建议1:摆脱Quartz并使用Spring Batch,因为它与Spring Boot集成良好

Counter argument 1: Spring Batch is overkill for simple tasks. 反论点1:对于简单任务,Spring Batch是过大的。 Use @Scheduled 使用@预定

Suggestion 2: Use @Scheduled annotations and cron expressions provided by spring 建议2:使用spring提供的@Scheduled批注和cron表达式

Counter argument 2: Your application will not be future ready if you remove Quartz. 反参数2:如果删除Quartz,您的应用程序将无法将来使用。 Complex scheduling may be required in the future 将来可能需要复杂的计划

Suggestion 3 : Use the spring interface ApplicationContextAware. 建议3:使用spring界面ApplicationContextAware。

Counter argument 3: Lots of additional code. 反参数3:许多其他代码。 Defeats the simple and easy concept of Spring boot 击败了Spring Boot的简单概念

Is there a simpler way in Spring Boot to access property file values and autowire objects in a class that implements a Quartz job (In this situation , the NightJob class) Spring Boot中有没有更简单的方法来访问实现Quartz作业的类中的属性文件值和自动装配对象(在这种情况下, NightJob类)

如注释中所述,Spring通过提供setter方法来支持将Bean注入Quartz作业: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM