【问题标题】:Issue with Quartz and Spring BootQuartz 和 Spring Boot 的问题
【发布时间】:2018-12-09 12:20:27
【问题描述】:

所以我遇到了一个需要帮助的技术挑战。

一个大型项目正在使用 Quartz 调度程序来安排一个作业在每晚 9 点运行。
计划的作业,但是需要从属性文件中读取值,使用自动装配等获取一些 bean。

当我使用@Autowired 和@Value 注释时,我发现值是空的。

问题是 Quartz 在 spring 容器外使用 newJob() 创建 JobDetail 对象。如下代码所示。

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

包装NightJobjobDetail 对象因此无法使用spring 访问属性文件或bean。

这是我的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{
    }
}

我扫描了 Stack Overflow 并列出了几个解决方案。我还阅读了 cmets 并列出了顶级计数器 cmets。

建议一:摆脱 Quartz,使用 Spring Batch,因为它与 Spring Boot 的良好集成

反论点 1: Spring Batch 对于简单的任务来说太过分了。使用@Scheduled

建议二:使用spring提供的@Scheduled注解和cron表达式

反论点 2:如果您删除 Quartz,您的应用程序将无法在未来做好准备。未来可能需要复杂的调度

建议三:使用spring接口ApplicationContextAware。

反论点 3: 大量附加代码。打败了 Spring boot 的简单易懂的概念

在 Spring Boot 中是否有更简单的方法来访问实现 Quartz 作业的类中的属性文件值和自动装配对象(在这种情况下,NightJob 类)

【问题讨论】:

标签: java spring spring-boot quartz-scheduler


【解决方案1】:

正如 cmets 中所写,Spring 通过提供 setter 方法支持将 bean 注入到 Quartz 作业中:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html

【讨论】:

  • 嗨,我按照上面的文档,但仍然得到 NullPointerException
猜你喜欢
  • 2021-04-05
  • 2016-10-10
  • 2019-03-25
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多