task-scheduling

Modules: Corn, Timer, ScheduledExecutor, Scheduled, Quartz, elastice-job Gitee Address (open source project) : gitee.com/flowerAndJa… Open source timer-Demo module: Timer timing case; ScheduledExecutor – Demo module: scheduled tasks for the scheduledExecutor thread pool. Springboot-scheduled module: Springboot built-in scheduled tasks; Quartz – Monomer – Demo module: Quartz achieves single task scheduling; Quartz cluster-Demo module: Quartz cluster building task scheduling; Elasticjob module: ElasticJob integrated with Spring; Elasticjob – Springboot-starter module: customize elasticJob startup class. Elasticjob-test module: Springboot tests scheduled tasks using ElasticJob.

Refer to the documentation below for knowledge modules;

A corn

Indicates the execution time of the task

1.1 how length

For example, 0 * * * *? Seven domains minute-hour day month week year Six domains (common) minute-hour day month weekCopy the code

1.2 Value for each field

Special characters allowed in the field 0-59 seconds, - * / minute 0-59, - * / hour 0-23, - * / day 1-31, - * /? L W month 1-12 or Jan-dec, - * / week 1-7 or sun-sat, - * /? L # 1970-2099, - * / Note: Each field allows the above number and special characters. '-' represents numeric range ',' enumeration '*' represents any number 'and '/' represents incremental. '? 'Week' is used when expressing the day of the month? '. When expressing the day of the week, use '? '. Case: 0 0 10 * *? Trigger 0 */1 * * every day at 10 o 'clock? 0 0-5 14 * *? Triggers 0 0 17 every minute between 2 p.m. and 2:05 p.m. every day? * TUES,THUR,SAT Every Tuesday, Thursday and Saturday at 5pmCopy the code

1.3 Online generation of corn expression generation verification

https://cron.qqe2.com/
Copy the code

Two Timer

Task scheduling provided by the JDK. Each Timer corresponds to a thread, so multiple timers can be started at the same time to execute multiple tasks in parallel. The tasks in the same Timer are executed in serial. It has been replaced by ScheduledExecutor, just know that there is a Timer.

Three ScheduledExecutor

Java 5 introduced timed tasks based on thread pool design. Each scheduled task takes out a thread from the thread pool and executes concurrently without interfering with each other.

Summary Timer and ScheduledExecutor can only execute how long the delay is, and execute in a certain period. Complex task scheduling cannot be completed.

Four Quartz

Supports complex scheduled tasks. Cluster is supported, but not distributed.

1.4.1 overview

1. Core role (Trigger, JobDetail, and Scheduler) Trigger: defines scheduling rules. JobDetail: encapsulates jobs, and jobs define specific services. Scheduler: Registers Trigger and JobDetail to the Scheduler. The Scheduler invokes the business in JobDetail based on the Trigger rule.

2. Single task scheduler uses the scheduler name in the configuration file to define the job class inherits QuartzJobBean and defines JobDetail and Triggerd objects in the configuration class

1.4.2 Cluster Service Establishment (Mysql Distributed Lock Required)

1. In a cluster environment, how can I prevent task duplication? A There is only one Scheduler instance per Quartz service. B A Scheduler instance requires qrTZ_locks in the table to execute tasks. Lock occupied: wait until another Scheduler releases the lock; Triggers: Obtain the trigger triggering table from qrTZ_TRIGGERS in Waiting state and is triggering. Change the Trigger state from Waiting to Acquired; Insert trigger information into the QrTZ_ fire_triggers table; C qrtz_fired_triggers changes to Executing; To perform the Job; Change the next triggering time in qrTZ_triggers; Change the state to Waiting;

2. Fault Diagnosis The Scheduler of each Quartz service periodically updates the LAST_CHECK_TIME field in the QrTZ_scheduler_state table. When one Scheduler in the cluster updates, the system checks whether other schedulers update as expected. If no, the node is considered faulty.

3. Failed migration deletes schedular information from the QrTZ_scheduler_state table so that the task can be performed by other schedular;

5 Elastic- Job (important, often used)

Support distributed task scheduling technology.

1. Perform the following steps: A Install the Zookeerper registry b Import the ElasticJob startup class c Custom listener, sharding rule (use default sharding rule directly) c write task class

1. Elastic- Job-Lite provides lightweight distributed tasks in jar packages. Relying on ZooKeeper: Distributed coordination and task information management; ; Sharding: minimum unit of execution for a scheduled task; This timed task method is executed several times when a project is divided into several pieces. So you might have a question mark, and that would create duplication of tasks. We handle our own business and divide tasks into different pieces. Shards are then put into different services. That is, the implementation of distributed scheduling, and do not repeat the execution of the task.

You can also customize the sharding policy for elasticJob. Need to inherit JobShardingStrategy. (For details, see ElasticJob-test.) Add a service for elastic capacity expansion. When ZooKeeper detects that a new service is registered, the service re-elects the master node, and the master node invokes the sharding policy to re-fragment services. Failover slices are distributed among several servers. If one service crashes, the fragment is preempted by other services. Ensure mission integrity. If this function is enabled, failover=”true” Idempotency A task is ready to be executed, but the scheduled task has not been completed. Do not execute this task and wait for the next task. MonitorExecution =true, turn on idempotency.