Quartz (quartz frame) theory

basic concept

Definition: task scheduling open source framework

Task scheduling:

In enterprise-level applications, some "scheduled tasks" are often formulated, that is, to do something at a certain point in time.

  • The core is time as the focus, that is, at a specific point in time, the system performs a specified operation.
  • Task scheduling involves multi-thread concurrency, thread pool maintenance, run-time rule analysis, protection and recovery of the running site, etc.

The Quartz framework is an open source enterprise-level task scheduling service, which has been regarded as a good solution for task scheduling.

Quartz highly abstracts task scheduling, proposes three core concepts, and describes them through interfaces and classes in the org.quartz package

Task: It is the content of the work to be performed. Quartz provides Job interface to support task definition

Trigger: Define the time trigger rules that trigger Job execution. Quartz provides the Trigger class and its subclasses to support trigger functions

Scheduler: Quartz provides a Scheduler interface to bind work tasks and triggers to ensure that tasks can be executed at the correct time

Example:

1. Import coordinate dependencies:

 <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>1.8.3</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz-jobs</artifactId>
      <version>2.2.3</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.36</version>
    </dependency>

2. Create an implementation class that implements Job:

public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("TriggerName:"+jobExecutionContext.getTrigger().getName());
        System.out.println("TriggerTime:"+new Date());
        System.out.println("JobName:"+jobExecutionContext.getJobDetail().getName());
        System.out.println("妈妈喊你回家吃饭");
    }
}

3. Specifically enable task scheduling:

//TODO:任务
        try {
            //1.定义任务对象
            JobDetail job = new JobDetail("task1","miaoGroup", MyJob.class);
            //2.创建SimpleTrigger触发器(间隔触发)
            SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1", "miaoTrigger",SimpleTrigger.REPEAT_INDEFINITELY,3000);
            //2.创建CronTrigger触发器(特定触发)
            CronTrigger cronTrigger = new CronTrigger("trigger1","miaoTrigger","0 58 10 ? * *");
            //3.创建调度器
            SchedulerFactory factory = new StdSchedulerFactory();
            Scheduler scheduler = factory.getScheduler();
            scheduler.scheduleJob(job,simpleTrigger);
            //4.启动调度器
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

SimpleTrigger: Define the interval time node trigger

CronTrigger: Define a specific time node trigger

Cron expression: consists of 6~7 time elements separated by spaces. The seventh element is optional.

Location field meaning scope special characters allowed
1 Second 0~59 *_/
2 minute 0~59 *_/
3 Hour 0~23 *_/
4 day of the month 1~31 *_/?L
5 month 1~12 or JAN~DEC *_/
6 Day of the week 1~7 or SUN~SAT * / ? L #
7 years 1970~2099 * /

Each field in a Cron expression can explicitly specify a value (such as 49), a range (1-6), a list (1, 3, 5) or a wildcard (such as *)

Cron expressions have several special characters, which are described as follows:

"-": a dash, indicating a range

",": Use comma-separated data to represent a list

"*": means every value, it can be used for all fields. Example: Express hourly in the hour field

"?": This character is only used in the "day of month" field and "day of week" field, indicating that no value is specified

"/": usually expressed as x/y, x is the starting value, and y represents the growth of the value

"L": Indicates the last day, used only in date and week fields

"#": It can only be used in the "day of the week" field, indicating the first few days of the month. Example: "6#3" refers to the third Friday of the month

exclude time rule

It can be implemented using the Calendar interface provided by Quartz, and Quartz provides a variety of implementation classes to meet the needs of the application

Calendar name effect
WeeklyCalendar Used to exclude one or more days of the week
MonthlyCalendar Used to exclude days of the month
AnnualCalendar Used to exclude one or more days of the year
HolidayCalendar Used to exclude holidays

Note: The Calendar interface is provided by quartz, not the calendar class under the java.util package in jdk

Example: exclude the first day of every Monday

//TODO:任务
        try {
            //1.定义任务对象
            JobDetail job = new JobDetail("task1","miaoGroup", MyJob.class);
            //2.创建SimpleTrigger触发器(间隔触发)
//            SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1", "miaoTrigger",SimpleTrigger.REPEAT_INDEFINITELY,3000);
            //2.创建CronTrigger触发器(特定触发)
            CronTrigger cronTrigger = new CronTrigger("trigger1","miaoTrigger","0 32 11 ? * * ");
            //2.1排除
            AnnualCalendar annualCalendar = new AnnualCalendar();
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH,1);
            calendar.set(Calendar.DATE,1);
            annualCalendar.setDayExcluded(calendar,true);
            //3.创建调度器
            SchedulerFactory factory = new StdSchedulerFactory();
            Scheduler scheduler = factory.getScheduler();
            scheduler.scheduleJob(job,cronTrigger);
            scheduler.addCalendar("exclued",annualCalendar,true,true);
            //4.启动调度器
            scheduler.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

Guess you like

Origin blog.csdn.net/qq_51404651/article/details/124792809