首页 文章

Quartz和Spring Boot问题

提问于
浏览
1

所以我有一个技术挑战,我需要帮助 .

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

当我使用@Autowired和@Value注释时,我发现值为null .

问题是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并列出了几个解决方案 . 我还阅读了评论并列出了顶级反评论 .

建议1:摆脱Quartz并使用Spring Batch,因为它与Spring Boot Counter参数1的良好集成:Spring Batch对于简单的任务来说太过分了 . 使用@Scheduled Suggestion 2:使用Spring提供的@Scheduled注释和cron表达式计数器参数2:如果删除Quartz,您的应用程序将不会在将来就绪 . 将来可能需要进行复杂的调度建议3:使用spring接口ApplicationContextAware . 反驳论点3:许多额外的代码 . 击败简单易用的Spring启动概念

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

1 回答

相关问题