Timing schedule

As back-end developers, we are always faced with business scenarios like synchronizing a batch of data every week; Check server health every half hour; Send users an email every morning at 8:00 with a todo list for the day, etc.

Each of these scenarios relies on a “timer”, which is like an alarm clock with a set time rule. It will trigger at a specified time to execute the scheduled task we want to define. So today we’re going to count all The Times that we’ve used over the years.

1. job (oracle)

I have been using Oracle database since I started working, and the first scheduled task I knew was The Job of Oracle database. A job can execute tasks at a specified point in time or at a certain point in time every day. After the Oracle database is restarted, the job continues to run.

In addition, the job mechanism is complete. You can query related tables or views, and query the timing rules and execution status of the job. The disadvantage is that as a tool at the Oracle database level, it is difficult to customize function expansion and secondary development.

1.1 create a job



1.2 remove the job



1.3 query job



2. crontab (linux)

Crond is a Linux daemon that periodically performs a task or waits for an event to be processed. Similar to scheduled tasks, crond is installed by default after the operating system is installed, and the Crond process is automatically started. The Crond process checks periodically every minute to see if there is a task to execute, and if there is a task to execute, it automatically executes it.

Cron is the service name, Crond is the background process, and crontab is the customized scheduled task table. Most Linux systems have Cron installed by default, so check it out.



Crontab Basic operation command



Crontab expression format



3. Timer and ScheduledExecutorService (Java)

Timer is a tool provided in the JDK. When used, a separate thread is created outside the main thread to execute the specified scheduled task. You can specify the execution of the task once or repeatedly.



TimerTask is an abstract class that implements the Runnable interface and represents a task that can be executed by a Timer. The TimerTask class is an abstract class that is scheduled by the Timer to be executed once or repeatedly. It has an abstract method run() method that performs the actions to be performed by the corresponding timer task. So each concrete task class must inherit TimerTask and override the run() method. It also has two non-abstract methods



Of course, Timer is rarely used, because its disadvantages are obvious:

  1. Single thread: When multiple timers run at the same time, it waits for one to complete before executing the next.
  2. The Timer thread does not catch exceptions. If the TimerTask throws an unchecked exception, the Timer thread will terminate.

Therefore, ScheduledExecutorService is generally used instead of Timer. ScheduledExecutorService: a scheduled task class built into the JDK based on thread pools. Each scheduled task is assigned to a thread in the thread pool, so the tasks are executed concurrently without affecting each other.

4. SpringTask (spring)

Timer and ScheduledExecutorService are both classes that belong to the JDK level to implement timing scheduling. Their functions are not enough to satisfy us. Now we introduce a relatively perfect timing scheduling tool – SpringTask, which is provided by Spring and supports annotations and configuration files. Crontab expression support, simple to use but powerful. I personally like SpringTask very much, simply because it supports crontab expressions.

Using springBoot is very simple:

  1. The startup class adds an annotation to enable scheduled scheduling @enablesCheduling
  2. Add annotations @scheduled (cron =”crontab expressions “) to methods that require Scheduled execution

There are only two simple steps to use by default, but the default use of SpringTask has some shortcomings:

  1. The default thread pool poolsize is 1, which can be interpreted as a timer-like single-threaded mode.
  2. The crontab expression cannot be dynamically modified. The modified crontab expression takes effect only after redeployment.

The solution to problem 1 is to modify the current thread pool by customizing TaskExecutor. Problem 2, the threadPoolTaskScheduler class can be directly used to implement custom scheduling rules.

Tasktimer.class: tasktimer.class: tasktimer.class












5. Quartz

Quartz is an open source job scheduling framework written entirely in Java that provides a simple yet powerful mechanism for scheduling jobs in Java applications. It is a powerful and mature heavyweight product that supports load balancing and distributed scheduling.

However, you’ll need to put a little extra effort into installing Quartz, from which tables to build your database to how your application will be deployed. For such a large product, this article does not include its instruction manual.


My entrepreneurial team product MadPecker, mainly do BUG management, test management, application distribution, website :www.madpecker.com, friends in need welcome to try, experience! This article is prepared by MadPecker team technical staff, please indicate the source of reprint