Extract:www.cnblogs.com/youzhibing/…

Java implementation of task scheduling

  • Timer
  • ScheduledExecutor
  • Spring Scheduler
  • Quartz

Timer

I believe that everyone has been useful, I have also used, but not much;Copy the code

Features: easy to use, but because all tasks are scheduled by the same thread, so all tasks are executed in serial, only one task can be executed at a time, the delay or exception of the previous task will affect the next task; Simple scheduled tasks can be implemented; slightly more complex (or demanding) scheduled tasks are not.

ScheduledExecutor

I'm sure you've all used this too, more than Timer; Because of Timer's shortcomings, Java 5 introduced ScheduledExecutor based on thread pool design;Copy the code

Features: Each scheduled task is executed by one thread in the thread pool, so the tasks are executed concurrently without interference from each other. Note that ScheduedExecutor actually starts a thread only when the execution time of the task arrives. The rest of the time ScheduledExecutor polls the status of the task. Although ScheduledExecutor and Calendar can be used to implement complex task scheduling, it is still cumbersome to implement and not friendly to development.

Spring Scheduler

Spring supports the implementation of task scheduling. The execution time of tasks can be specified, but the control of task queues and thread pools is weak. Generally integrated into projects, small tasks are very convenient.Copy the code

JCronTab

JCronTab is a Java task scheduling tool written completely according to crontab syntax.

Features:

The execution time of the task can be specified.

Provide uniX-POSIX crontab format to specify time.

Support a variety of task scheduling persistence methods, including ordinary files, databases and XML files for persistence;

JCronTab has the built-in email function, which can easily send the task execution result to the person who needs to be notified.

Design and deployment are high-performance and scalable.

Quartz

In this paper, the protagonist

Related concepts of Quartz

Scheduler: a Scheduler to schedule tasks; Job: business Job, also known as the business component; This interface needs to be implemented for the execution of scheduled tasks. The scheduler will invoke the execute method of this interface to complete our scheduled service JobDetail: An instance of a Job that is defined as a quartz Job. Many times we refer to Job as a JobDetail Trigger, which defines when a given Job is executed JobBuilder: Job builder, which defines or creates instances of JobDetail; JobDetail specifies only instances of jobs TriggerBuilder: TriggerBuilder used to define or create instances of triggers

Specific why should be divided so fine, we can look up the relevant information, you will find a lot of things

Project code

Code cloud Address:Gitee.com/XiaoCaiNiao…

If you want to configure a separate data source for Quartz, you can use @Quartzdatasource to configure the data source. See spring-boot-2.0.3 for Quartz integration, data source issues, source code exploration)

The Trigger condition

Table name, column name of each table, trigger state, trigger type, etc

  • -Dan: I’m WAITING
  • ACQUIRED: will be triggered, at this time it is not yet the actual trigger moment of the trigger
  • EXECUTING a EXECUTING EXECUTING EXECUTING EXECUTING trigger the real time
  • COMPLETE: no longer triggered
  • BLOCKED: BLOCKED, not allowed concurrently job occurs (@ DisallowConcurrentExecution)
  • ERROR: ERROR
  • PAUSED: PAUSED
  • PAUSED_BLOCKED: pause, not allowed concurrently job occurs (@ DisallowConcurrentExecution)
  • DELETED: indicates a deletion
  • MISFIRED: Trigger failure is deprecated. There is an alternative
  • The flow chart of state change is as follows

    The initial state of the trigger is WAITING, and the trigger in WAITING state is WAITING to be triggered. The scheduling thread will continuously scan the Triggers table and pull the trigger to be triggered in advance according to NEXT_FIRE_TIME. If this trigger is pulled by the scheduling thread, its state will become ACQUIRED. Since the trigger is pulled in advance and the real trigger moment of the trigger is not reached, the scheduling thread will wait until the real trigger moment and then change the trigger state from required to EXECUTING. If the trigger is no longer executed, the state is changed to COMPLETE, otherwise to WAITING, and a new cycle begins. If any part of this cycle throws an exception, the trigger’s state changes to ERROR. If you pause this trigger manually, the state will change to PAUSED.

conclusion

Quartz is an open source job scheduling framework that offers great flexibility without sacrificing simplicity. We can use it to create simple or complex schedules for executing a job. It has many features, such as: database, clustering, plug-ins, JavaMail support, EJB job prebuild, support for Cron-like expressions, and more;

Springboot is very easy to integrate with Quartz. In the simplest case, just import our dependencies and enjoy the features provided by Quartz. Springboot will configure Quartz for us by default. Of course, we can also customize the configuration to implement quartz customization