preface

Several readers recently wrote me asking me about their business scenarios and what kind of timed tasks they would use. Indeed, we have a lot of options for using different timed tasks in different business scenarios. I’m here to give you a list of 10 very useful timed tasks, and there’s always one for you.

1. Scheduled tasks in Linux

crontab

If you’ve ever encountered a situation where you need to temporarily tally up online data and export it to an Excel spreadsheet. This requirement is sometimes too complex to be met by simply writing SQL statements, which requires Java code. Then the program into a JAR package, the online environment to execute, and finally the generated Excel file downloaded to the local.

In order to reduce the impact on the online environment, we usually choose to execute statistical procedures at 1 or 2 am when the number of users is low. (Actually, around 4 a.m., users are the least.)

Since it was so late, there was no need for us to wait for the execution results. A timed task could have been done.

So what kind of timing task is better for this situation?

Answer: Crontab for Linux. (Although some projects are not deployed on Linux)

Run crontab -e to edit the timer and add the following command:

` 0 2 * * * / usr/local/Java/jdk1.8 / bin/Java jar/data/app/tool. The jar > / logs/tool. The log & `Copy the code

The tool. Jar program can be executed at 2 a.m. every day and logs can be exported to the tool.log file. Of course, you can also write the following command to execute the Java program as shell script, more convenient maintenance.

You can use the scheduled task support to modify the periodic rules and manage the configured periodic scripts in a unified manner.

The format of the crontab command is as follows:

'crontab [parameter] [file name]'Copy the code

If no file name is specified, the command typed on the keyboard is received and loaded into crontab.

The comparison table of parameter functions is as follows:

parameter

function

-u

Specify a user

-e

Edit the crontab file of a user

-l

The crontab file of a user is displayed

-r

Example Delete the crontab file of a user

-i

This parameter is required when deleting the crontab file of a user

If no user is specified using -u, the current user is used by default.

Run the crontab -e command to edit the file content. The syntax is as follows:

'[minute] [hour] [date] [month] [week] Specific task'Copy the code

Among them:

  • The number of minutes ranges from 0 to 59
  • Hour: indicates the number of hours. The value ranges from 0 to 23
  • Date, which indicates the specific day. Range: 1-31
  • Month: indicates the number of months. The range is 1-12
  • Week: number of weeks. Range: 0-7. Both 0 and 7 represent Sundays

There are also special characters such as:

  • *Stands for how time, for example:* 1 * * *The command is executed at 1:00 a.m. every day.
  • /Represents how often it is executed, for example:* / 5 * * * *The command is executed every 5 minutes.
  • .Delegates can be multiple, for example:10 7,9,12 * * *This parameter is executed at 7, 9, and 12:10 every day.
  • -Stands for supporting a range, such as:10 7-9 * * *This parameter is executed at 7:00, 8:00, and 9:10 every day.

Crond is a daemon that periodically performs certain tasks on Linux. The Crond service tool is installed by default after Linux is installed, and the Crond service starts automatically by default. The Crond process checks periodically every minute to see if there is a task to execute, and if there is, it executes it automatically.

You can run the following command to operate related services:

'service crond status // Check the running status.' 'service crond start // Start the service.' 'service crond stop // Stop the service.' 'service crond restart // Restart the service 'service crond reload // Reload configuration'Copy the code

usecrontabAdvantages and disadvantages:

  • Advantages: It is convenient to modify timing rules and supports some complex timing rules. You can centrally manage various timing scripts by using files.
  • Disadvantages: If the scheduled task is very many, it is not easy to find, and the operating system must belinuxOtherwise, the command cannot be executed.

JDK built-in scheduled tasks

1.Thread

Thread class can do timed tasks. If you look at the source code for some of the timed task frameworks, you’ll eventually see that they also use the Thread class at the bottom.

The code for implementing such a timed task is as follows:

`public static void init() {` `new Thread(() -> {` `while (true) {` `try {` `System.out.println("doSameThing"); ` `Thread.sleep(1000 * 60 * 5); ` `} catch (Exception e) {` `log.error(e); ` `}` `}` `}).start(); ` ` `}Copy the code

We can use the Thread class to do the simplest of timed tasks, with a while loop in the run method (among other things) to perform our own tasks. Try… try… try… Catch catches an exception, or if it does, it simply exits the loop and will not be able to continue next time.

Scheduled tasks in this mode can only be executed periodically, but cannot be executed at a specified point in time.

In addition, this thread can be defined as a daemon thread that executes silently in the background.

Usage scenarios: for example, sometimes a project needs to download a file every 10 minutes, or read a template file every 5 minutes to generate a static HTML page, etc., some simple periodic task scenarios.

useThreadClass pros and cons:

  • Pros: These timed tasks are simple, cheap to learn and easy to get started, making them a great choice for simple, periodic tasks.
  • Disadvantages: Does not support the execution of tasks at a specified point in time, does not support delayed execution and other operations, the function is too simple, cannot cope with some complex scenarios.

2.Timer

The Timer class is used by the JDK to schedule the execution of specified tasks in background threads. In the java.util package, it is used in conjunction with TimerTask.

The Timer class is a task scheduler that contains a TimerThread thread that loops indefinitely to retrieve timerTasks from a TaskQueue (the class implements the Runnable interface), calls its run method, and asynchronously executes scheduled tasks. We need to inherit the TimerTask class, implement its Run method, and add our own business logic to that method.

The code for implementing such a timed task is as follows:

`public class TimerTest {` `public static void main(String[] args) {` `Timer timer = new Timer(); ` `timer.schedule(new TimerTask() {` `@Override` `public void run() {` `System.out.println("doSomething"); ` `} ` `}, 2000100, 0). ` ` `} ` `}Copy the code

A Timer class is instantiated, then its Schedule method is called, where the TimerTask class is instantiated, with the business logic written in the Run method. The last two parameters of the schedule method represent the delay time and the interval time in milliseconds. In the preceding example, the scheduled task is executed every second with a delay of 2 seconds.

There are 6 methods:

  • schedule(TimerTask task, Date time)The task is executed at the specified time
  • schedule(TimerTask task, long delay)The task is executed after the specified delay
  • schedule(TimerTask task, Date firstTime,long period)After task is executed at firstTime, the task repeats the execution of peroid with a fixed delay frequency
  • schedule(TimerTask task, long delay, long period)Task executes peroid repeatedly after the specified delay is specified
  • scheduleAtFixedRate(TimerTask task,Date firstTime,long period)After task is executed at firstTime, the task repeats the execution of peroid with a fixed delay frequency
  • scheduleAtFixedRate(TimerTask task, long delay, long period)Task executes peroid repeatedly after the specified delay is specified

But the use ofTimerThe following problems occur when implementing a scheduled task:

  1. Due to theTimerIf a task takes a long time, the execution of other tasks will be affected.
  2. ifTimerTaskthrowRuntimeException, Timer will stop all tasks.

useTimerClass pros and cons:

  • Advantages: very convenient implementation of multiple periodic periodic tasks, and support delayed execution, but also support after the specified time, the function is still powerful.
  • Disadvantages: If one task takes a long time, the execution of other tasks will be affected. And if theTimerTaskthrowRuntimeException.TimerStops all tasks running, so it is not recommended in the Alibaba developer specification.

3.ScheduledExecutorService

ScheduledExecutorService is a scheduled task introduced in JDK1.5+ and is located under java.util.concurrent and distributed packages.

ScheduledExecutorService is based on multithreading. It is designed to solve the problem that the Timer is executed by a single thread and multiple tasks will affect each other.

It consists of four main methods:

  • schedule(Runnable command,long delay,TimeUnit unit), the schedule with delay is executed only once, after which the task can be blocked by future.get () until it is finished.
  • schedule(Callable<V> callable,long delay,TimeUnit unit)After the schedule, the task can be blocked through future.get () until the task is completed and the execution result can be obtained.
  • scheduleAtFixedRateIs a task that is executed at a fixed frequency. If the current task takes too much time and exceeds the periodic period, the task is executed immediately after the current task ends.
  • scheduleWithFixedDelay, indicates that the task is executed with a fixed delay. The delay is calculated from the end of the current task to the start time.

The code for implementing such a timed task is as follows:

`public class ScheduleExecutorTest {` `public static void main(String[] args) {` `ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5); ` `scheduledExecutorService.scheduleAtFixedRate(() -> {` `System.out.println("doSomething"); ` `}, 1000100, TimeUnit. MILLISECONDS); ` ` `} ` `}Copy the code

The ScheduledExecutorService class scheduleAtFixedRate method is called to implement periodic tasks. The tasks are executed every second and delayed by 1 second each time.

This kind of timed task is used to replace the Timer class in the Alibaba developer specification. It is a good choice for multi-threading to perform periodic tasks.

Advantages and disadvantages of ScheduledExecutorService:

  • Advantages: Based on multi-threaded scheduled tasks, there is no correlation between multiple tasks, support periodic task execution, and with the delay function.
  • Disadvantages: Does not support some complex timing rules.

Spring supports scheduled tasks

1.spring task

Spring Task is a scheduled task delivered with Spring 3 or later. To implement the scheduled task function, you need to import the Spring-Context package. Currently, spring Task supports XML and annotations.

1. Project practice

Since the XML approach is so old, let’s take the annotation approach in the SpringBoot project as an example.

The first step is to introduce spring-context-related dependencies in the POM.xml file.

`<dependency>`
 `<groupId>org.springframework</groupId>`
 `<artifactId>spring-context</artifactId>`
`</dependency>`
Copy the code

Second, annotate the springBoot boot class with @enablesCheduling.

`@EnableScheduling` `@SpringBootApplication` `public class Application {` `public static void main(String[] args) {` `new SpringApplicationBuilder(Application.class).web(WebApplicationType.SERVLET).run(args); ` ` `} ` `}Copy the code

Third, use the @scheduled annotation to define timing rules.

`@Service` `public class SpringTaskTest {` `@Scheduled(cron = "${sue.spring.task.cron}")` `public void fun() {` `System.out.println("doSomething"); ` ` `} ` `}Copy the code

The fourth step, in the applicationContext. Configuration parameters in the properties file:

`sue.spring.task.cron=*/10 * * * * ? `Copy the code

This allows fun to be executed every 10 seconds.

2. The cron rules

In versions above spring4, the cron expression contains six parameters:

'[seconds] [minute] [hour] [date] [month] [week]'Copy the code

Several commonly used special symbols are also supported:

  • *: indicates that a task is triggered at any time
  • .: indicates that a task is triggered at a specified time
  • -: indicates that a task is triggered within a period of time
  • /: indicates the starting point and the interval at which a task is triggered.
  • ?: indicates two sub-expressions used for days of the month and week, indicating that no value is specified.

Cron expression parameters:

  1. Value range: 0-59. The value is supported*,.,-,/.
  2. Value range: 0-59. This parameter is supported*,.,-,/.
  3. “, the value ranges from 0 to 23*,.,-,/.
  4. Value range: 1 to 31. The value is supported*,.,-,/. More than the second?, if specifiedweekIf yes, it is configuredThe date ofBecomes invalid.
  5. Month: Value range: 1 to 12 this parameter is supported*,.,-,/.
  6. The value of week ranges from 1 to 7. 1 indicates Sunday, 6 indicates Saturday, and so on. support*,.,-,/,?. More than the second?, if specifiedThe date ofIf yes, it is configuredweekBecomes invalid.

Examples of common cron expressions

  • 0, 0, 0, 1 star?It is executed at 00:00 on the first day of each month
  • 0, 0, 2 * *, right?Every day at 2 a.m
  • 0, 0, 2 * *, right?Every day at 2 a.m
  • 0 0/5, 11 * *, right?Every five minutes from 11:00-11:55 every day
  • 0 0 18? * WEDEvery Wednesday at 6 PM

Spring task by ScheduledAnnotationBeanPostProcessor processScheduled method first, parse and collect Scheduled the parameter annotation, include: cron expression.

Then, in the ScheduledTaskRegistrar class’s afterPropertiesSet method, initialize a single-threaded ThreadPoolExecutor to perform the task by default.

If you are interested in Spring Task, please add me to wechat to chat with me privately.

usespring taskAdvantages and disadvantages:

  • Advantages: The spring framework has its own timing function, SpringBoot has done a very good encapsulation, it is very easy to start and define the timing task, support complexcronExpression, can meet the vast majority of standalone version of the business scenario. For a single task, the next task is scheduled after the current task is scheduled.
  • Disadvantages: Single thread by default. If the execution time of the previous task is too long, the execution of the later task will be affected. Scheduled data store tasks cannot be performed because cluster deployment is not supported.

2.spring quartz

Quartz is an open source project of OpenSymphony in the field of Job Scheduling. It is an open source task scheduling system developed by Java.

What can Quartz do?

  • Job scheduling: Invokes job scripts of various frameworks, such as shell and Hive.
  • Timed task: Perform the task you want to perform at a predetermined time.

The architecture diagram is as follows:

Quartz includes the following main interfaces:

  • SchedulerRepresents a scheduling container in which multiple Jobdetails and triggers can be registered.
  • JobRepresents the work, that is, the concrete content to be performed.
  • JobDetailRepresents a specific executable scheduler, and Job is what the executable scheduler will execute.
  • JobBuilderUsed to define or build JobDetail instances.
  • TriggerRepresents a scheduling trigger that decides when to call.
  • TriggerBuilderUsed to define or build triggers.
  • JobStoreUsed to store the status of jobs and tasks during scheduling.

1. Project practice

Let’s take springboot integration with Quartz as an example.

The first step is to introduce Quartz related dependencies in the POM.xml file.

`<dependency>`
 `<groupId>org.springframework.boot</groupId>`
 `<artifactId>spring-boot-starter-quartz</artifactId>`
`</dependency>`
Copy the code

Second, create a real scheduled task execution class that inherits QuartzJobBean.

`public class QuartzTestJob extends QuartzJobBean {` `@Override` `protected void executeInternal(JobExecutionContext context) throws JobExecutionException {` `String userName = (String) context.getJobDetail().getJobDataMap().get("userName"); ` `System.out.println("userName:" + userName); ` ` `} ` `}Copy the code

Step 3, create the scheduler JobDetail and the scheduler Trigger.

`@Configuration` `public class QuartzConfig {` `@Value("${sue.spring.quartz.cron}")` `private String testCron; Public JobDetail quartzTestDetail() {' 'JobDetail JobDetail = JobBuilder.newJob(QuartzTestJob.class)` `.withIdentity("quartzTestDetail", "QUARTZ_TEST")` `.usingJobData("userName", "susan")` `.storeDurably()` `.build(); ` `return jobDetail; Public Trigger quartzTestJobTrigger() {// CronScheduleBuilder is executed every 5 seconds cronScheduleBuilder = CronScheduleBuilder.cronSchedule(testCron); ` ` / / create a Trigger ` ` Trigger, the Trigger. = TriggerBuilder newTrigger () ` `. ForJob (quartzTestDetail ()) ` `.withIdentity("quartzTestJobTrigger", "QUARTZ_TEST_JOB_TRIGGER")` `.withSchedule(cronScheduleBuilder)` `.build(); ` `return trigger; ` ` `} ` `}Copy the code

The fourth step, in the applicationContext. Configuration parameters in the properties file:

`sue.spring.quartz.cron=*/5 * * * * ? `Copy the code

This will execute the executeInternal method of the QuartzTestJob class every five seconds.

CronTrigger configuration format:

'[second] [minute] [hour] [day] [month] [week] [year]'Copy the code

Spring Quartz is basically the same as spring Task’s cron expression rules, except that spring4 or later removes the following year, while Quartz’s CronTrigger year is optional, so I won’t go into details here.

usespring quartzAdvantages and disadvantages:

  • Advantage: The default is multi-thread asynchronous execution, a single task, in the last scheduling is not completed, the next scheduling time, another thread will start a new scheduling, multiple tasks do not affect each other. Support complexcronExpression, which can be instantiated by a cluster and supports distributed deployment.
  • Disadvantages: Compared with Spring Task, it costs more to implement scheduled tasks and requires manual configurationQuartzJobBean,JobDetailandTriggerAnd so on. Need to bring in a third partyquartzPackage, there is a certain learning cost. It does not support parallel scheduling, failure processing, and dynamic fragmentation policies.

4. Distributed scheduled tasks

1.xxl-job

Xxl-job is a distributed task scheduling platform developed by Dianping (Xu Xueli). Its core design goal is rapid development, simple learning, lightweight and easy to expand. Now open source and access to many companies online product lines, out of the box.

The XXL-Job framework extends Quartz to use the mysql database to store data and has jetty built in as an RPC service call.

The main features are as follows:

  1. There is an interface to maintain scheduled tasks and trigger rules, which is easy to manage.
  2. Can start or stop tasks dynamically
  3. Supports flexible capacity expansion and reduction
  4. Support task failure alarm
  5. Dynamic sharding support
  6. Failover support
  7. Rolling real-time log
  8. Supports user and permission management

Management interface:

The overall architecture diagram is as follows:

The architecture diagram using Quartz is as follows:

The project of actual combat

Xxl-admin management background deployment and mysql script execution and other preparatory work, I will not introduce more, there is a need for friends can find me private chat, these more inclined to the operation and maintenance of things.

Assuming that the previous work is OK, we need to:

First, introduce xxl-job-related dependencies in the POM.xml file.

`<dependency>`
 `<groupId>com.xuxueli</groupId>`
 `<artifactId>xxl-job-core</artifactId>`
`</dependency>`
Copy the code

The second step, the applicationContext. Configuration parameters in the properties file:

`xxl.job.admin.address: http://localhost:8088/xxl-job-admin/`
`xxl.job.executor.appname: xxl-job-executor-sample`
`xxl.job.executor.port: 8888`
`xxl.job.executor.logpath: /data/applogs/xxl-job/`
Copy the code

Create a HelloJobHandler class that inherits the IJobHandler class:

`@JobHandler(value = "helloJobHandler")` `@Component` `public class HelloJobHandler extends IJobHandler {` `@Override` `public ReturnT<String> execute(String param) {` `System.out.println("XXL-JOB, Hello World."); ` `return SUCCESS; ` ` `} ` `}Copy the code

The scheduled task is then configured.

You are advised to deploy scheduled tasks to another service, separate from the API service. Based on my experience, most jobs perform batch operations on data. If the data volume is too large, the memory and CPU resources of the service may be affected.

usexxl-jobAdvantages and disadvantages:

  • Advantages: Supports scheduled task management, elastic capacity expansion and reduction, dynamic sharding, failover, and failure alarm. Its function is very powerful, many large factories in use, can meet the vast majority of business scenarios.
  • Disadvantages: andquartzSimilarly, database distributed locks are used to control tasks from being executed repeatedly. There are some performance issues in the case of a large number of tasks.

2.elastic-job

Elastice-job is an elastic and distributed task scheduling system developed by Dangdang. It uses ZooKeeper to implement distributed coordination, high availability and task sharding. It is specifically developed for high concurrency and complex business scenarios.

Elastic – the job is a subproject under the apache shardingsphere project, website address: shardingsphere.apache.org/elasticjob/…

Elastic-job after 2.x, there were two product lines: Elastic-Job-Lite and Elastic-Job-Cloud, and we used elastic-Job-Lite to meet our needs. Elastik-job-lite is positioned as a lightweight, decentralized solution that uses JAR packages to coordinate distributed tasks, relying only on Zookeeper.

The main features are as follows:

  • Distributed scheduling coordination
  • Elastic capacity expansion and reduction
  • Failure to transfer
  • Missed execution job retrigger
  • Job sharding consistency ensures that only one execution instance of the same sharding in a distributed environment
  • Self-diagnose and fix problems caused by distributed instability
  • Support parallel scheduling

Overall architecture Diagram:

The project of actual combat

The first step is to introduce elastice-job dependencies in the pom.xml file.

`<dependency>`
 `<groupId>com.dangdang</groupId>`
 `<artifactId>elastic-job-lite-core</artifactId>`
`</dependency>`
`<dependency>`
 `<groupId>com.dangdang</groupId>`
 `<artifactId>elastic-job-lite-spring</artifactId>`
`</dependency>`
Copy the code

Step 2: Add ZKConfig class and configure ZooKeeper:

`@Configuration` `@ConditionalOnExpression("'${zk.serverList}'.length() > 0")` `public class ZKConfig {` `@Bean` `public  ZookeeperRegistryCenter registry(@Value("${zk.serverList}") String serverList,` `@Value("${zk.namespace}") String namespace) {` `return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace)); ` ` `} ` `}Copy the code

Step 3: Define a class that implements the SimpleJob interface:

`public class TestJob implements SimpleJob {` `@Override` `public void execute(ShardingContext shardingContext){` `System.out.println("ShardingTotalCount:"+shardingContext.getShardingTotalCount()); ` `System.out.println("ShardingItem:"+shardingContext.getShardingItem()); ` ` `} ` `}Copy the code

Step 4: Add the JobConfig configuration task:

`@Configuration` `public class JobConfig {` `@Value("${sue.spring.elatisc.cron}")` `private String testCron; ` `@Value("${sue.spring.elatisc.itemParameters}")` `private String shardingItemParameters; ` `@Value("${sue.spring.elatisc.jobParameters}")` `private String jobParameters =; ` `@Value("${sue.spring.elatisc.shardingTotalCount}")` `private int shardingTotalCount; ` `@Autowired` `private ZookeeperRegistryCenter registryCenter; ` `@Bean` `public SimpleJob testJob() {` `return new TestJob(); ` `}` `@Bean` `public JobScheduler simpleJobScheduler(final SimpleJob simpleJob) {` `return new SpringJobScheduler(simpleJob, registryCenter, getConfiguration(simpleJob.getClass(),` `cron, shardingTotalCount, shardingItemParameters, jobParameters)); ` `}` `private geConfiguration getConfiguration(Class<? extends SimpleJob> jobClass,String cron,int shardingTotalCount,String shardingItemParameters,String jobParameters) {` `JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), testCron, shardingTotalCount).` `shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).build(); ` `SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName()); ` `LiteJobConfiguration jobConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build(); ` `return jobConfig; ` ` `} ` `}Copy the code

Among them:

  • Cron: A CRON expression that defines the triggering rule.
  • ShardingTotalCount: Defines the total number of job fragments
  • ShardingItemParameters: defines the allocation item parameters. Generally, the fragment sequence number and parameters are separated by equal signs, multiple key-value pairs are separated by commas, and the fragment sequence number starts from 0 and cannot be greater than or equal to the total number of job fragments.
  • JobParameters: user-defined jobParameters

Fifth, the applicationContext. Configuration parameters in the properties file:

`spring.application.name=elasticjobDemo`
`zk.serverList=localhost:2181`
`zk.namespace=elasticjobDemo`
`sue.spring.elatisc.cron=0/5 * * * * ?`
`sue.spring.elatisc.itemParameters=0=A,1=B,2=C,3=D`
`sue.spring.elatisc.jobParameters=test`
`sue.spring.elatisc.shardingTotalCount=4`
Copy the code

In this case, scheduled tasks are configured. The procedure for creating scheduled tasks is more complicated than xxL-job.

useelastic-jobAdvantages and disadvantages:

  • Advantages: support distributed scheduling coordination, support sharding, suitable for high concurrency, and some business is relatively complex scenarios.
  • Disadvantages: Need to rely on ZooKeeper to implement scheduled tasks relative toxxl-jobIt’s a little bit more complicated, you have to be very familiar with sharding rules.

3. Other distributed scheduled tasks

1. Saturn

Saturn is an open-source distributed task scheduling platform from Vipshop. It replaces the traditional Linux Cron/Spring Batch Job method to achieve unified configuration, unified monitoring, high availability of tasks, and concurrent processing of fragments.

Saturn is based on Dangdang’s open-source Elastice-Job, combined with the needs of all parties and our practical insights. Use cases: Vipshop, Kugou Music, Xinwang Bank, Hailongyi, AirMedia Online, Liofu Zhengxin, etc.

Github address: github.com/vipshop/Sat…

2. TBSchedule

TBSchedule is a distributed task scheduling platform developed by Alibaba, which aims to separate scheduling jobs from business systems, reduce or eliminate the coupling degree with business systems, and carry out efficient asynchronous task processing.

At present, it is widely used in the process scheduling system of Many Internet enterprises such as Alibaba, Taobao, Alipay, JINGdong, Jumei, Autohome and Gome.

Github address: github.com/taobao/TBSc…

To be honest, there are a lot of good timed tasks out there. It’s not that we have to use the best timed tasks, but we have to choose according to the actual business needs. Each scheduled task has its advantages and disadvantages. It is the best policy to choose a proper task to meet business requirements and avoid resource waste. Of course, in actual service scenarios, multiple scheduled tasks are used together.