After the release of the new version of SpringBoot2.0, I was eager to try something new and changed the relevant project to the latest version. I saw the update log on GitHub source code of SpringBoot, indicating that AutoConfiguration has been automatically configured for the new version of Quartz. Saves a lot of tedious configuration.

Official website update log

Auto-configuration support is now included for the Quartz Scheduler. We’ve also added a new spring-boot-starter-Quartz starter POM. You can use in-memory JobStores, or a full JDBC-based store. All JobDetail, Calendar and Trigger beans from your Spring application context will be automatically registered with the Scheduler. For more details read the new “Quartz Scheduler” section of the reference documentation.

SpringBoot2.0 integrates with the latest version of Quartz2.3.0.

Objective in this chapter

Automated Quartz configuration with new SpringBoot2.0 features.

SpringBoot Enterprise core technology learning project


project Project name Project description
001 Spring Boot core technology Explain some of the core components of SpringBoot at the enterprise level
002 Spring Boot core technology chapter source code Spring Boot core technology book each article code cloud corresponding source code
003 Spring Cloud core technology Full presentation of Spring Cloud core technologies
004 Spring Cloud core technology chapter source code Spring Cloud core technology brief book each article corresponding source code
005 QueryDSL core technology Full introduction to QueryDSL core technology and SpringDataJPA integration based on SpringBoot
006 SpringDataJPA core technology Full presentation of SpringDataJPA core technology
007 SpringBoot Core technology learning directory SpringBoot system learning directory, please focus on praise!! !

Build the project

In the previous chapter, Chapter 40: Implementing Scheduled Task Distributed Multi-node Load Persistence based on SpringBoot & Quartz, we have completed the integration by adding configuration. For the convenience of this chapter, we have directly copied the previous project and modified it on the basis. Open the pm. XML configuration file and SpringBoot provides corresponding dependencies for us. We delete the previous Quartz dependencies and replace them with spring-boot-starter-Quartz, as shown below:

<! Scheduler </artifactId> </artifactId> <version>${quartz.version}</version>
</dependency>
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz-jobs</artifactId>
	<version>${quartz.version}</version> </dependency> >>>> replace with: >>>> <! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ......Copy the code

Delete the QuartzConfiguration configuration class

In the previous section, we used the QuartzConfiguration class to complete a series of configurations for Quartz, such as: JobFactory, SchedulerFactoryBean, etc., do not need to actively declare the factory class after we add the spring-boot-starter-Quartz dependency, because spring-boot-starter-Quartz is already configured for us automatically.

Automatic configuration source code

External Libraries: spring-boot-autoconfigure-2.0.0.release.jar Find the org. Springframework. Boot. Autoconfigure. Quartz, the directory is SpringBoot provide us quartz automation configuration source, in that directory are shown in the following several kinds:

  • AutowireCapableBeanJobFactoryThis class replaces our previous one inQuartzConfigurationConfiguration of the classAutowiringSpringBeanJobFactoryThe inner class implementation, the main role is our customQuartzJobBeanSubclasses areSpring IOCFor hosting, you can use injection anywhere in the scheduled task classSpring IOCManaged classes.
  • JobStoreTypeThis class is an enumerated type that defines the correspondingapplication.yml,application.propertiesIn the filespring.quartz.job-store-typeConfiguration, whose purpose is to configurequartzThe data storage modes of tasks are as follows: MEMORYThe default), JDBC (database mode).
  • QuartzAutoConfigurationThis class is the main class for automatic configuration and is configured internallySchedulerFactoryBeanAs well asJdbcStoreTypeConfiguration, the use ofQuartzPropertiesAutomate configuration conditions as properties.
  • QuartzDataSourceInitializerThis class is mainly used for some operations after data source initialization. Different database scripts are selected based on different platform types of databases.
  • QuartzPropertiesThis class corresponds tospring.quartzinapplication.yml,application.propertiesConfiguration at the beginning of the file.
  • SchedulerFactoryBeanCustomizerThis is an interface that we implement and use with the implementation classSpring IOCHosting can be doneSchedulerFactoryBeanPersonalized Settings, the Settings here can be completely rightSchedulerFactoryBeanMake all Settings changes.

Spring. The quartz configuration

See QuartzAutoConfiguration source code, we know, to use automatic configuration, need to meet the QuartzProperties property configuration class initialization, Therefore, we need to add the corresponding configuration information to the application.yml and application.properties configuration files, as follows:

spring:
  quartz:
    # Configure related attributes
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            isClustered: true
            clusterCheckinInterval: 10000
            useProperties: false
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
    # database mode
    job-store-type: jdbc
    Initialize the table structure
    #jdbc:
      #initialize-schema: never
Copy the code
  • spring.quartz.propertiesThis configuration actually replaces the previous onequartz.properties, we put beforequartz.propertiesAll configurations in the configuration file are converted toYUMLStyle, corresponding to the configuration can be added in theQuartzAutoConfigurationClass, is automatically calledSchedulerFactoryBeanthesetQuartzPropertiesThe method,spring.quartz.propertiesAll the configurations inside are set.
@Bean
@ConditionalOnMissingBean
public SchedulerFactoryBean quartzScheduler() { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory(this.applicationContext.getAutowireCapableBeanFactory())); // If spring.quartz. Properties is configuredif(! This. The properties. The getProperties (). The isEmpty ()) {/ / set all the properties to QuartzProperties schedulerFactoryBean.setQuartzProperties(this.asProperties(this.properties.getProperties())); }... Omit some codeCopy the code
  • spring.quartz.job-store-typeSet up thequartzThe data persistence mode of the task, by default, is memory mode, we use the previous mode, configurationJDBCPersist tasks using a database.
  • spring.quartz.jdbc.initialize-schema

This configuration is not currently in effect. According to the documentation on the website, it is intended to automatically initialize the data tables required by Quartz.

test

  1. Start the project
  2. Open a browserhttp://localhost:8083/good/save?name=abcd&unit= jin & price = 12.5Add a scheduled task
  3. View console output
22:55:18. 17161-812 the INFO [main] c. engyu. Chapter39. Chapter47Application: 【【【【【【 Scheduled task distributed node- Quartz -cluster-node-second Started 】】】】】】 2018-03-06 22:55:20.772 INFO 17161 -- [uartzScheduler]] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now, After delay of 2 seconds 22:55:20 2018-03-06. 17161-793 the INFO [uartzScheduler]] org. Quartz. Core. QuartzScheduler: Scheduler quartzScheduler_$_yuqiyudeMacBook-pro.local1520348117910 started. 2018-03-06 22:56:20.103 INFO 17161 -- [NIO-8083-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet'dispatcherServlet'The 2018-03-06 22:56:20. 17161-103 the INFO [nio - 8083 - exec - 1] O.S.W eb. Servlet. DispatcherServlet: FrameworkServlet'dispatcherServlet': initialization started the 2018-03-06 22:56:20. 121 INFO - 17161 [nio - 8083 - exec - 1] O.S.W eb. Servlet. DispatcherServlet: FrameworkServlet'dispatcherServlet': initialization completed in 18 ms
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? wherenext_val=? Hibernate: insert into basic_good_info (bgi_name, bgi_price, bgi_unit, bgi_id) values (? ,? ,? ,?) The 2018-03-06 22:56:20. 17161-268 TRACE [nio - 8083 - exec - 1] O.H.T ype. Descriptor. SQL. BasicBinder: Binding Parameter [1] as [VARCHAR] - [ABCD] 2018-03-06 22:56:20.269 TRACE 17161 -- [NIO-8083-exec-1] o.h.type.descriptor.sql.BasicBinder : Binding Parameter [2] as [NUMERIC] - [12.5] 2018-03-06 22:56:20.269 TRACE 17161 -- [NIO-8083-EXEC-1] o.h.type.descriptor.sql.BasicBinder : Binding parameter [3] as [VARCHAR] - [catty] 2018-03-06 22:56:20.269 TRACE [NIO-8083-exec-1] o.h.type.descriptor.sql.BasicBinder : Binding parameter [4] as [BIGINT] - [1] 2018-03-06 22:56:47.253 INFO 17161 -- [eduler_worker-1] c.h.c.timers.GoodStockCheckTimer : Distributed node Quartz-cluster-node-second: performs scheduled inventory check tasks. Execution time: Tue Mar 06 22:56:47 CST 2018 22:57:00 2018-03-06. 17161-012 the INFO [eduler_Worker - 2] C.H.C.T imers. GoodStockCheckTimer: Distributed node Quartz-cluster-node-second: performs scheduled inventory check tasks. Execution time: Tue Mar 06 22:57:00 CST 2018 2018-03-06 22:57:20.207 INFO 17161 -- [eduler_worker-3] c.hengyu.chapter39.timers.GoodAddTimer : Distributed node Quartz cluster-node-second, execute the task after commodity is added. Task time: Tue Mar 06 22:57:20 CST 2018 22:57:30 2018-03-06. 17161-013 the INFO [eduler_Worker - 4] C.H.C.T imers. GoodStockCheckTimer: Distributed node Quartz-cluster-node-second: performs scheduled inventory check tasks. Execution time: Tue Mar 06 22:57:30 CST 2018 22:58:00 2018-03-06. 17161-014 the INFO [eduler_Worker - 5] C.H.C.T imers. GoodStockCheckTimer: Distributed node Quartz cluster-node-second: Performs a scheduled inventory check task at Tue Mar 06 22:58:00 CST 2018Copy the code

According to the console content, it can be seen that our scheduled task has started to execute normally. Of course, if we open multiple nodes, we can also achieve the effect of automatic task drift.

conclusion

To conclude, we have SpringBoot2.0 Quartz integration done, we just need to add dependencies, add configurations, and do nothing else.

This chapter source code has been uploaded to the code cloud: SpringBoot matching source address: gitee.com/hengboy/spr… SpringCloud source code address: gitee.com/hengboy/spr… SpringBoot can be found in: directory: SpringBoot Learning directory: QueryDSL General Query Framework Learning directory: SpringDataJPA SpringDataJPA Learning directory, thanks for reading!

Welcome to join the knowledge planet, hengyu youth take you to walk the future technology road!!

Limited time special 66 yuan/year, join hengyu youth hardcore fans knowledge planet, here you want to learn the knowledge, Hengyu youth one on one to answer the difficult knowledge problems!!