简体   繁体   中英

Running a Spring Boot app with Quartz

I have this Spring Boot app. using Quartz (a richly featured, open source job scheduling library that can be integrated within virtually any Java application ) to execute a Job the 10th day of the month

@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 {
    ..
}
}

and inside the config package:

@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();               

    }
}

I run the app. from the command line, using mvn spring-boot:run but it seems that the Quartz is not initialised:

...
    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$ 

I also tried changing the expression to 0 * * * * ? (every minute) with the same result

and also created this other class with the same result:

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()));

    }
}

for your information, I have another similar application without Quartz properties and its working fine:

@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();               

    }
}

Have you considered using Springs own scheduler, it's easy to configure and I've always found it to work well. https://spring.io/guides/gs/scheduling-tasks/

You are missing various configuration for Quartz here like

  1. Quartz properties
  2. SchedulerFactoryBean

Follow below examples for complete implementation:

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

You may give a try to https://github.com/mejariamol/quartz-easy . This library simplifies the quartz scheduler integration in spring boot framework. I came up with this while setting up quartz scheduler in one of the projects at work.

  1. Annotate your Job implementation with Scheduled annotation with required configuration by setting suitable fields present in the annotation.
import com.indusnode.quartz.annotation.Scheduled;
...
@Scheduled(interval="5", intervalType=Scheduled.IntervalType.SEC)
class TestJob implements Job {
    //...
}
  1. Set application property qe.base-package as your base package name of the project which will contain all your job implementations. Also, add com.indusnode as a value to basePackage in component scan.
  2. And you are done! No need to define factories, JobDetail, Triggers...etc.

To use this library, include the quartz-easy artifact in the dependencies section of your pom.xml

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

For details, please refer https://search.maven.org/artifact/com.indusnode/quartz-easy/1.0.0/jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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