Distributed task scheduling (00)--Quartz

1 Overall process of task scheduling

2 components

  • Scheduler: The factory class creates a Scheduler and schedules tasks according to the time rules defined by the trigger.
  • Task: Job represents the scheduled task
  • Trigger: Trigger defines the element of scheduling time and the time rules according to which tasks are executed. A Job can be associated with multiple Triggers, but a Trigger can only be associated with one Job.
import org.quartz.*;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzDemo {
    public static void main(String[] args) throws SchedulerException {
        // 创建JobDetail对象
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("myJob", "group1")
                .build();

        // 创建Trigger对象
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

        // 创建Scheduler对象
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();

        // 将Job和Trigger注册到Scheduler
        scheduler.scheduleJob(jobDetail, trigger);
    }
}

Execute task scheduling core class QuartzSchedulerThread:

  1. The scheduling thread obtains the list of triggers that need to be executed from the JobStore and modifies the status of the triggers.
  2. Fire trigger, modify the trigger information (the next time the trigger is executed, and the trigger status), and store it
  3. Finally, create a specific execution task object and execute the task through the worker thread pool.

3 Cluster deployment solution

There is no node responsible for centralized management, but database row-level locks are used to achieve concurrency control.

The scheduler instance first acquires the row lock in the {0}LOCKS table in cluster mode, and MySQL acquires the row lock:

  • {0} will be replaced by the QRTZ_ configured by default in the configuration file
  • sched_name is the instance name of the application cluster
  • lock_name is the row-level lock name

1.3 Quartz’s row-level locks

  • Trigger access lock (TRIGGER_ACCESS)
  • State access lock (STATE_ACCESS)

It solves the problem of distributed scheduling of tasks. Only one node can run the same task, and other nodes will not execute the task. When encountering a large number of short tasks, each node frequently competes for database locks. The more nodes there are, the worse the performance.

This article is published by OpenWrite, a blog that publishes multiple articles !

OpenAI opens ChatGPT to all users for free. Voice programmers tampered with ETC balances and embezzled more than 2.6 million yuan a year. Spring Boot 3.2.0 was officially released. Google employees criticized the big boss after leaving the company. He was deeply involved in the Flutter project and formulated HTML-related standards. Microsoft Copilot Web AI will be Officially launched on December 1st, supporting Chinese Microsoft's open source Terminal Chat Rust Web framework Rocket releases v0.5: supports asynchronous, SSE, WebSockets, etc. The father of Redis implements the Telegram Bot framework using pure C language code . If you are an open source project maintainer, encounter How far can you endure this kind of response? PHP 8.3 GA
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/3494859/blog/10141421