Spring Boot Integrated quartz cluster environment for dynamic Timing task Configuration "original"

Source: Internet
Author: User

Recently made a spring boot integration quartz to implement dynamic scheduled task configuration, running in a clustered environment. Be able to do the task, the dynamic of the increase and deletion check, the interface is as follows:

1. Introduction of Jars in the project

2. Import the required tables into the database

The official web has a different database of scripts, to find the corresponding, import can

3. Java code

Configure the relevant configuration file for Quartz to expose the bean for later reference.

One of the key points is to inject the spring context, or it can be considered a pit. If the spring context is not injected, then the newly added scheduled task job, which is a new thread, does not have any relationship with spring and does not invoke the spring-injected service method in the job.

To inject a spring context, the job must be instantiated in this way

@Configurationpublic class Druiddbconfig {    @Primary    @Bean (name = "DataSource")    @ Configurationproperties (prefix = "Spring.datasource")    public DataSource DataSource () {        return  Datasourcebuilder.create (). build (); }

4. Configuration runs in a clustered environment
The Isclustered property is set to True, which is equivalent to telling Quartz that it is currently in a clustered environment.

  /** * Set quartz properties * * @author XW * @create_date December 28, 2016 * @return Properties*/     PublicProperties Quartzproperties () {Properties Prop=NewProperties (); //The Org.quartz.scheduler.instanceName property can be any value, used in the JDBC jobstore//to uniquely identify the instance, but must be the same in all cluster nodes. Prop.put ("Quartz.scheduler.instanceName","Serverscheduler"); //The InstanceId property is auto, which generates the instance ID based on the host name and timestamp. Prop.put ("org.quartz.scheduler.instanceId","AUTO"); //        //Quartz has a built-in "update check" feature, so quartz items are checked on the official website each time they are started, quartz if there is a new version.        This check is asynchronous and does not affect the startup and initialization of the quartz project itself. //set the property of Org.quartz.scheduler.skipUpdateCheck to True to skip the update checkProp.put ("Org.quartz.scheduler.skipUpdateCheck","false"); //Prop.put ("Org.quartz.scheduler.jobFactory.class","org.quartz.simpl.SimpleJobFactory"); //The Org.quartz.jobStore.class property is Jobstoretx, which persists the task to the data. Because the nodes in the cluster rely on the database to propagate//Scheduler The state of the instance, you can only apply the Quartz cluster when using JDBC jobstore. //This means that you must use JOBSTORETX or JOBSTORECMT as the Job store; you cannot use Ramjobstore in a cluster. Prop.put ("Org.quartz.jobStore.class","Org.quartz.impl.jdbcjobstore.JobStoreTX"); Prop.put ("Org.quartz.jobStore.driverDelegateClass","org.quartz.impl.jdbcjobstore.StdJDBCDelegate"); Prop.put ("Org.quartz.jobStore.dataSource","Quartzdatasource"); Prop.put ("Org.quartz.jobStore.tablePrefix","Qrtz_"); //The isclustered property is true, and you tell the scheduler instance to participate in a cluster. This attribute is used throughout the scheduling framework to alwaysProp.put ("org.quartz.jobStore.isClustered","true"); //        //The Clustercheckininterval property defines the frequency, in milliseconds, that the scheduler instance is checked into the database. Scheduler Check if other instances are not checked in when they should be checked in;//This can indicate a failed Scheduler instance, and the current Scheduler will take over any Job that fails and is recoverable. //with the check-in operation, Scheduler also updates its own status record. The smaller the clusterchedkininterval, the Scheduler//the more frequently a node checks for Scheduler instances that fail. The default value is 15000 (that is, 15 seconds)Prop.put ("Org.quartz.jobStore.clusterCheckinInterval","60000"); Prop.put ("Org.quartz.threadPool.class","Org.quartz.simpl.SimpleThreadPool"); Prop.put ("Org.quartz.threadPool.threadCount","5"); Prop.put ("Org.quartz.dataSource.quartzDataSource.driver","Com.mysql.jdbc.Driver"); Prop.put ("Org.quartz.dataSource.quartzDataSource.URL", Datasourceurl); Prop.put ("Org.quartz.dataSource.quartzDataSource.user", username); Prop.put ("Org.quartz.dataSource.quartzDataSource.password", password); Prop.put ("org.quartz.dataSource.quartzDataSource.maxConnections","Ten"); returnprop; }

Spring Boot Integrated quartz cluster environment for dynamic Timing task Configuration "original"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.