Sample code of spring integrating quartz distributed scheduling

preface

In order to ensure high availability and high concurrency of applications, multiple nodes are generally deployed; For scheduled tasks, if each node executes its own scheduled tasks, on the one hand, it consumes system resources,

On the other hand, some tasks are executed many times, which may cause application logic problems. Therefore, a distributed scheduling system is needed to coordinate each node to execute scheduled tasks.

Spring integrates quartz

Quartz is a mature task scheduling system. Spring is compatible with quartz to facilitate development. Let's see how to integrate it:

1. Maven dependency file

Mainly spring related libraries, quartz libraries and MySQL driver libraries. Note: distributed scheduling requires databases, and MySQL is selected here;

2. Configure job

There are two ways to configure a job: methodinvokingjobdetailfactorybean and jobdetailfactorybean

2.1MethodInvokingJobDetailfactorybean

It is used when calling a method of a specific bean. The specific configuration is as follows:

2.2JobDetailfactorybean

This method is more flexible. You can set transfer parameters as follows:

The task class defined by jobclass inherits quartzjobbean and implements the executeinternal method; Jobdatamap is used to pass data to jobs

3. Configure triggers for scheduling

Two trigger types are also provided: simpletriggerfactorybean and crontrigerfactorybean

Focus on the crontrigerfactorybean, which is more flexible, as follows:

The job specified in JobDetail is the job configured in step 2. Cronexpression is configured to execute the job every 5 seconds;

4. Configure the schedulerfactorybean of quartz scheduler

Two methods are also provided: memory RAMJobStore and database

4.1 ram jobstore

Job related information is stored in memory. Each node stores its own and is isolated from each other. The configuration is as follows:

4.2 database mode

Job related information is stored in the database. All nodes share the database. Each node communicates through the database to ensure that a job will only be executed on one node at the same time, and

If a node hangs, the job will be assigned to other nodes for execution. The specific configuration is as follows:

Datasource is used to configure data sources and data table related information. You can download GZ package from the official website. The SQL file is in the path: Docs \ dbtables, which provides SQL files of mainstream databases;

Configlocation configured quartz The properties file is in quartz Jar. Org Under the quartz package, it provides some default data, such as org quartz. jobStore. class

Here you need to convert quartz The properties are copied and modified as follows:

5. Related categories

Firsttask inherits quartzjobbean, implements executeinternal method and calls FirstService

FirstService needs to provide a serialization interface because it needs to be saved in the database;

The main class is used to load the quartz configuration file;

Test distributed scheduling

1. Start the app twice at the same time and observe the log:

A1 has log output, A2 does not; When A1 is stopped, A2 has log output;

2. Add new jobs: secondtask and secondservice, add relevant configuration files, start the app twice, and observe the log:

A1 log is as follows:

A2 log is as follows:

It can be found that both A1 and A2 have execution tasks, but the same task can only be executed at one node at the same time, and can only be assigned to other nodes after the execution is completed;

3. If the interval is less than the task execution time, for example, it is changed to sleep (6000)

A1 log is as follows:

A2 log is as follows:

The interval time is 5 seconds, while the task execution takes 6 seconds. By observing the log, it can be found that the task has not ended and a new task has started. This situation may lead to application logic problems, in fact, whether the task can support serialization;

4. The @ disallowcurrentexecution annotation ensures the serialization of tasks

Add @ disallowcurrentexecution annotation on firsttask and secondtask respectively. The log results are as follows:

A1 log is as follows:

A2 log is as follows:

By observing the log, it can be found that the task will start a new task only after end, realizing the serialization of the task;

summary

This article aims to have an intuitive understanding of Spring + quartz distributed scheduling and solve problems through practical use. Of course, there may be many questions, such as how it is scheduled, what happens if the database hangs, and so on. We still need to have a deeper understanding.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>