使用Quartz运行Spring Boot应用程序

时间:2017-10-31 14:16:29

标签: java spring spring-mvc spring-boot quartz-scheduler

我有这个Spring Boot应用程序。使用Quartz(一个功能丰富的开源作业调度库,可以集成在几乎任何Java应用程序中),以便在每月的第10天执行作业

@EnableScheduling
@SpringBootApplication
public class IberiaUtilsApplication implements CommandLineRunner {

public static void main(String[] args) {
        SpringApplication app = new SpringApplication(IberiaUtilsApplication.class);
        app.run(args);
    }

    @Override
    public void run(String... args) throws Exception {
    ..
}
}

并在配置包中:

@Configuration
public class JobConfig {

    @Bean
    public JobDetail sampleJobDetail() {
        return JobBuilder.newJob(MonthlyIberiaMetrics.class).withIdentity("iberiaJob")
                .usingJobData("iberiaMetrics", "fleet").storeDurably().build();
    }

    @Bean
    public Trigger sampleJobTrigger() {

        return newTrigger()
                .forJob(sampleJobDetail())
                .withIdentity("iberiaTrigger")
                .withSchedule(cronSchedule("0 0 10 10 * ?"))
                .build();               

    }
}

我运行应用程序。从命令行,使用mvn spring-boot:run,但似乎Quartz没有初始化:

...
    017-10-31 15:11  [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
    2017-10-31 15:11  [main] WARN  c.z.hikari.util.DriverDataSource - Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
    2017-10-31 15:12  [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
    2017-10-31 15:12  [main] INFO  org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
    2017-10-31 15:12  [main] INFO  o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
    2017-10-31 15:12  [main] INFO  org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.0 created.
    2017-10-31 15:12  [main] INFO  org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
    2017-10-31 15:12  [main] INFO  org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.0) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
      Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
      NOT STARTED.
      Currently in standby mode.
      Number of jobs executed: 0
      Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
      Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

    2017-10-31 15:12  [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
    2017-10-31 15:12  [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.0
    2017-10-31 15:12  [main] INFO  org.quartz.core.QuartzScheduler - JobFactory set to: org.springframework.boot.autoconfigure.quartz.AutowireCapableBeanJobFactory@64ea8964
    2017-10-31 15:12  [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
    2017-10-31 15:12  [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Bean with name 'dataSource' has been autodetected for JMX exposure
    2017-10-31 15:12  [main] INFO  o.s.j.e.a.AnnotationMBeanExporter - Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
    2017-10-31 15:12  [main] INFO  o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 2147483647
    2017-10-31 15:12  [main] INFO  o.s.s.quartz.SchedulerFactoryBean - Starting Quartz Scheduler now
    2017-10-31 15:12  [main] INFO  org.quartz.core.QuartzScheduler - Scheduler quartzScheduler_$_NON_CLUSTERED started.
    2017-10-31 15:12  [Thread-3] INFO  o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@589ebb: startup date [Tue Oct 31 15:11:56 CET 2017]; root of context hierarchy
    2017-10-31 15:12  [Thread-3] INFO  o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase 2147483647
    2017-10-31 15:12  [Thread-3] INFO  org.quartz.core.QuartzScheduler - Scheduler quartzScheduler_$_NON_CLUSTERED paused.
    2017-10-31 15:12  [Thread-3] INFO  o.s.s.quartz.SchedulerFactoryBean - Shutting down Quartz Scheduler
    2017-10-31 15:12  [Thread-3] INFO  org.quartz.core.QuartzScheduler - Scheduler quartzScheduler_$_NON_CLUSTERED shutting down.
    2017-10-31 15:12  [Thread-3] INFO  org.quartz.core.QuartzScheduler - Scheduler quartzScheduler_$_NON_CLUSTERED paused.
    2017-10-31 15:12  [Thread-3] INFO  org.quartz.core.QuartzScheduler - Scheduler quartzScheduler_$_NON_CLUSTERED shutdown complete.
    2017-10-31 15:12  [Thread-3] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown
    2017-10-31 15:12  [Thread-3] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans
    2017-10-31 15:12  [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
    2017-10-31 15:12  [Thread-3] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
    MacBook-Pro-de-lopes:iberiaUtils lopes$ 

我也尝试使用相同的结果将表达式更改为0 * * * * ?(每分钟)

并且还创建了另一个具有相同结果的类:

public class ScheduledTasks1 {


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(cron = "0 * * * * ?")
    public void reportCurrentTime() {

        System.out.println("The time is now {}" +
                dateFormat.format(new Date()));

    }
}

为了您的信息,我有另一个类似的应用程序没有Quartz properties并且工作正常:

@SpringBootApplication
public class IberiaReservationsApplication {

    public static void main(String[] args) {
        SpringApplication.run(IberiaReservationsApplication.class, args);
    }

    @Bean
    public JobDetail sampleJobDetail() {
        return JobBuilder.newJob(CheckDBJobExecution.class).withIdentity("sampleJob")
                .usingJobData("name", "World").storeDurably().build();
    }

    @Bean
    public Trigger sampleJobTrigger() {
            return newTrigger()
                .forJob(sampleJobDetail())
                .withIdentity("sampleTrigger")
                .withSchedule(cronSchedule("0 0/2 8-17 * * ?"))
                .build();               

    }
}

3 个答案:

答案 0 :(得分:2)

您是否考虑过使用Springs自己的调度程序,它易于配置,而且我总能发现它运行良好。 https://spring.io/guides/gs/scheduling-tasks/

答案 1 :(得分:0)

您在这里缺少Quartz的各种配置,如

  1. 石英属性
  2. SchedulerFactoryBean来
  3. 请按照以下示例进行完整实施:

    1. https://chynten.wordpress.com/2016/06/17/quartz-with-databse-and-spring-4/
    2. http://www.baeldung.com/spring-quartz-schedule

答案 2 :(得分:0)

您可以尝试https://github.com/mejariamol/quartz-easy。该库简化了Spring Boot框架中的Crystal Scheduler集成。我在工作中的一个项目中设置石英调度程序时想到了这一点。

  1. 通过设置注释中存在的适当字段,使用计划的注释和所需的配置来注释Job实施。
import com.indusnode.quartz.annotation.Scheduled;
...
@Scheduled(interval="5", intervalType=Scheduled.IntervalType.SEC)
class TestJob implements Job {
    //...
}
  1. 将应用程序属性qe.base-package设置为项目的基本软件包名称,该项目将包含您的所有作业实现。另外,在组件扫描中,将com.indusnode作为值添加到basePackage。
  2. 您完成了!无需定义工厂,JobDetail,触发器...等。

要使用此库,请在quartz-easy的依赖项部分中包含pom.xml工件

<dependency>
  <groupId>com.indusnode</groupId>
  <artifactId>quartz-easy</artifactId>
  <version>1.0.0</version>
</dependency>

有关详细信息,请参阅https://search.maven.org/artifact/com.indusnode/quartz-easy/1.0.0/jar