【问题标题】:How to access application property in org.quartz.Job class?如何访问 org.quartz.Job 类中的应用程序属性?
【发布时间】:2019-03-27 07:06:34
【问题描述】:

我创建了一个 Spring Boot 应用程序来实现 Quartz 调度程序。在 Job 类中,我想使用 application.properties 中的一些属性。如何注入?

当我尝试使用值注释来使用它时,得到 null。

这是我的代码

主类:

@SpringBootApplication
public class SchedulerApplication {

    public static void main(String[] args) {

        SpringApplication.run(SchedulerApplication.class, args);

    }

    @Bean
    public Scheduler scheduler() {

        Scheduler scheduler = null;
        try {

            SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
            scheduler = stdSchedulerFactory.getScheduler();
            scheduler.start();


        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return scheduler;

    }

}

职位类别:

@Component
public class ScheduleJob implements org.quartz.Job {

    @Value("${ijobs.service.url}")
    public String ijobsUrl;

    public ScheduleJob() {

    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        String jobName = null;
        System.out.println("ijobsUrl--->"+ijobsUrl);
        } 
}

【问题讨论】:

    标签: spring spring-boot properties quartz-scheduler quartz


    【解决方案1】:

    尝试在 Job 类上使用此 @Order

      @Component
        @Order(Ordered.HIGHEST_PRECEDENCE + 99)
        public class ScheduleJob implements org.quartz.Job {
    
            @Value("${ijobs.service.url}")
            public String ijobsUrl;
    
            public ScheduleJob() {
    
            }
    
            public void execute(JobExecutionContext context) throws JobExecutionException {
                String jobName = null;
                System.out.println("ijobsUrl--->"+ijobsUrl);
                } 
    
      }
    

    【讨论】:

    • ijobsUrl--->null 得到 null :(
    • 通过类路径上的 quartz.properties 配置
    • ijobs.service.url ="/something" 将该属性添加到您的quartz.properties Properties p = new Properties(); p.load("/resources/quartz.properties"); // 属性文件的路径 System.out.println(p.getProperty("ijobs.service.url");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2013-02-24
    相关资源
    最近更新 更多