[Spring] Spring timing task notes @Scheduled original so simple [Spring] Spring timing task notes @Scheduled original so simple

Excerpt: https://www.cnblogs.com/larrydpk/p/11893942.html

[Spring] Spring timing task notes @Scheduled original so simple

1 Introduction

Timing of realization of the task is very large, JDK of the Timer , the Spring provides lightweight Scheduled Task , the Quartz and Linux Cron , etc., there are some distributed task scheduling framework. This paper describes the Scheduled Task use.

2 4 convenient ways

Annotations @Scheduledcan be used in the method of the following conditions:

(1) does not return type or return type void;

(2) no parameters;

Spring open the Scheduler is very simple, a comment @EnableSchedulingcan:

@Configuration
@EnableScheduling
public class SchedulingConfig { }

If the application is Springboot, directly in start class top with @EnableSchedulingready to use.

2.1 fixed delay fixedDelay

On behalf of the next task began with a task on the end of the interval is always fixed length of time, and always will wait on a task is completed, and will open the next task. If there is such a demand dependent claims, the use of this model is very suitable. code show as below:

@Scheduled(fixedDelay = 1000)
public void fixedDelay() { log.info("fixedDelay"); }

Parameter 1000, representing the fixed delay is 1000 milliseconds, or 1 second, so the output is:

2019-11-19 21:02:43,977 scheduling-1:fixedDelay 2019-11-19 21:02:44,981 scheduling-1:fixedDelay 2019-11-19 21:02:45,983 scheduling-1:fixedDelay 2019-11-19 21:02:46,984 scheduling-1:fixedDelay 

2.2 fixed frequency fixedRate

2.2.1 Normally

Frequency characteristics of a given task is the time to perform the task interval is always the same. For example, to perform once per hour, is the time to start the task execution time interval of 1 hour. code show as below:

@Scheduled(fixedRate = 2000)
public void fixedRate() { log.info("fixedRate"); }

Parameter 2000 is performed every two seconds, the output is:

2019-11-19 21:38:45,073 scheduling-1:fixedRate 2019-11-19 21:38:47,076 scheduling-1:fixedRate 2019-11-19 21:38:49,073 scheduling-1:fixedRate 2019-11-19 21:38:51,075 scheduling-1:fixedRate 

2.2.2 default single-threaded

Note that by default it is single-threaded, parallel execution is not. Even if the frequency is fixed, but the next task must wait until the task is completed on time will begin. The following example illustrates well:

@Scheduled(fixedRate = 1000)
public void fixedRateLongTimeTask() throws InterruptedException { log.info("fixedRateLongTimeTask"); Thread.sleep(3000); }

Since the tasks to be performed three seconds to complete, even if fixedRateset to 1 second, and can not be performed once every second, the output is as follows:

2019-11-19 21:46:00,108 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:03,113 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:06,113 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:09,117 scheduling-1:fixedRateLongTimeTask 

Output once every three times.

2.2.3 notes to help you @Async

The above problems have a solution? The answer is yes, and very simple. Just add a comment @Asyncyou can make the task can be executed asynchronously multi-threaded code is as follows:

@Async
@Scheduled(fixedRate = 1000)
public void fixedRateLongTimeTask() throws InterruptedException { log.info("fixedRateLongTimeTask"); Thread.sleep(3000); }

Can be seen through a log, even in front of the second task is not yet completed execution. And not the same thread name, executed by a multi-threaded, output is:

2019-11-19 21:54:22,261 task-5:fixedRateLongTimeTask 2019-11-19 21:54:23,257 task-6:fixedRateLongTimeTask 2019-11-19 21:54:24,257 task-4:fixedRateLongTimeTask 2019-11-19 21:54:25,257 task-8:fixedRateLongTimeTask 2019-11-19 21:54:26,259 task-1:fixedRateLongTimeTask 2019-11-19 21:54:27,262 task-2:fixedRateLongTimeTask 2019-11-19 21:54:28,260 task-3:fixedRateLongTimeTask 

Note : It should be noted that, as required @EnableScheduling, the need to configure add annotations @EnableAsyncto turn the function switch. In addition, if the task execution time is very long, for example, one minute, the situation is not the same. Details later @Asyncuse it.

2.3 Initial Delay initialDelay

The initial delay is used initialDelayto specify, which can delay the time of the first task execution. Examples of the following parameters is 30 seconds, 30 seconds after the start, before the first started. You can reduce the burden on the project started, before preparing the data can also perform the task.

@Scheduled(fixedDelay = 1000, initialDelay = 30*1000)
public void fixedDelayWithIntialDelay() { log.info("fixedDelayWithIntialDelay"); }

Output is as follows:

2019-11-19 22:10:02,092 main:Tomcat started on port(s): 443 (http) with context path '' 2019-11-19 22:10:02,095 main:Started DemoApplication in 1.272 seconds (JVM running for 1.767) 2019-11-19 22:10:32,063 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:33,067 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:34,069 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:35,069 scheduling-1:fixedDelayWithIntialDelay

It can be seen in about 30 seconds after the start of the project, began to perform the task.

2.4 Cron Expressions

Provided above functions and can not meet all the needs of scheduling regular tasks, such as the need to send text messages every month on the 1st, every Saturday for data analysis. Here Cron expression comes in handy.

The following example shows the number of seconds whenever 06 when executed. code show as below:

@Scheduled(cron = "6 * * ? * *")
public void cron() { log.info("cron"); }

The results are as follows:

2019-11-19 22:20:06,003 scheduling-1:cron 2019-11-19 22:21:06,004 scheduling-1:cron 2019-11-19 22:22:06,002 scheduling-1:cron 

Cron expression is very powerful, very rich information on the Internet, where not start speaking up.

3 parameters of

The previous example would die on the parameters written code, if a more flexible, in fact, can be configured with parameters. So we need to modify the parameters of time, without having to modify the code, compile and then deploy the package, directly modify configuration files.

code show as below:

@Scheduled(cron = "${pkslow.cron}")
public void cronWithConfig() { log.info("cronWithConfig"); }

In application.properties configured as follows:

pkslow.cron=* * * ? * *

Code executed once one second.

4 If she suddenly disappeared

Since the Spring of default Scheduler is single-threaded, so there will be a problem if a task execution stuck, it would not proceed with the down. Log on performance is suddenly disappeared. The probability of this happening is not small, such as the operation of the database deadlock, http request timeout to wait indefinitely, there is a deadlock and other causes.

When this happens, the command should jstack pid > pid.ThreadDump.txtget the current thread situation, then analyze whether it is stuck, stuck in which part, and then analyze specific code. Solved by providing a timeout or retry the like.

5 Conclusion

This paper describes the scheduled tasks comment Spring's @Scheduleduse, about the use and configure a variety of ways. It is very convenient and simple, for simple enough to cope with the task of timing.


Welcome to public concern number < pumpkin slow, said >, you will continue to update ...

file

More books, more sharing; and more writing, more than finishing.

 

1 Introduction

Timing of realization of the task is very large, JDK of the Timer , the Spring provides lightweight Scheduled Task , the Quartz and Linux Cron , etc., there are some distributed task scheduling framework. This paper describes the Scheduled Task use.

2 4 convenient ways

Annotations @Scheduledcan be used in the method of the following conditions:

(1) does not return type or return type void;

(2) no parameters;

Spring open the Scheduler is very simple, a comment @EnableSchedulingcan:

@Configuration
@EnableScheduling
public class SchedulingConfig { }

If the application is Springboot, directly in start class top with @EnableSchedulingready to use.

2.1 fixed delay fixedDelay

On behalf of the next task began with a task on the end of the interval is always fixed length of time, and always will wait on a task is completed, and will open the next task. If there is such a demand dependent claims, the use of this model is very suitable. code show as below:

@Scheduled(fixedDelay = 1000)
public void fixedDelay() { log.info("fixedDelay"); }

Parameter 1000, representing the fixed delay is 1000 milliseconds, or 1 second, so the output is:

2019-11-19 21:02:43,977 scheduling-1:fixedDelay 2019-11-19 21:02:44,981 scheduling-1:fixedDelay 2019-11-19 21:02:45,983 scheduling-1:fixedDelay 2019-11-19 21:02:46,984 scheduling-1:fixedDelay 

2.2 fixed frequency fixedRate

2.2.1 Normally

Frequency characteristics of a given task is the time to perform the task interval is always the same. For example, to perform once per hour, is the time to start the task execution time interval of 1 hour. code show as below:

@Scheduled(fixedRate = 2000)
public void fixedRate() { log.info("fixedRate"); }

Parameter 2000 is performed every two seconds, the output is:

2019-11-19 21:38:45,073 scheduling-1:fixedRate 2019-11-19 21:38:47,076 scheduling-1:fixedRate 2019-11-19 21:38:49,073 scheduling-1:fixedRate 2019-11-19 21:38:51,075 scheduling-1:fixedRate 

2.2.2 default single-threaded

Note that by default it is single-threaded, parallel execution is not. Even if the frequency is fixed, but the next task must wait until the task is completed on time will begin. The following example illustrates well:

@Scheduled(fixedRate = 1000)
public void fixedRateLongTimeTask() throws InterruptedException { log.info("fixedRateLongTimeTask"); Thread.sleep(3000); }

Since the tasks to be performed three seconds to complete, even if fixedRateset to 1 second, and can not be performed once every second, the output is as follows:

2019-11-19 21:46:00,108 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:03,113 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:06,113 scheduling-1:fixedRateLongTimeTask 2019-11-19 21:46:09,117 scheduling-1:fixedRateLongTimeTask 

Output once every three times.

2.2.3 notes to help you @Async

The above problems have a solution? The answer is yes, and very simple. Just add a comment @Asyncyou can make the task can be executed asynchronously multi-threaded code is as follows:

@Async
@Scheduled(fixedRate = 1000)
public void fixedRateLongTimeTask() throws InterruptedException { log.info("fixedRateLongTimeTask"); Thread.sleep(3000); }

Can be seen through a log, even in front of the second task is not yet completed execution. And not the same thread name, executed by a multi-threaded, output is:

2019-11-19 21:54:22,261 task-5:fixedRateLongTimeTask 2019-11-19 21:54:23,257 task-6:fixedRateLongTimeTask 2019-11-19 21:54:24,257 task-4:fixedRateLongTimeTask 2019-11-19 21:54:25,257 task-8:fixedRateLongTimeTask 2019-11-19 21:54:26,259 task-1:fixedRateLongTimeTask 2019-11-19 21:54:27,262 task-2:fixedRateLongTimeTask 2019-11-19 21:54:28,260 task-3:fixedRateLongTimeTask 

Note : It should be noted that, as required @EnableScheduling, the need to configure add annotations @EnableAsyncto turn the function switch. In addition, if the task execution time is very long, for example, one minute, the situation is not the same. Details later @Asyncuse it.

2.3 Initial Delay initialDelay

The initial delay is used initialDelayto specify, which can delay the time of the first task execution. Examples of the following parameters is 30 seconds, 30 seconds after the start, before the first started. You can reduce the burden on the project started, before preparing the data can also perform the task.

@Scheduled(fixedDelay = 1000, initialDelay = 30*1000)
public void fixedDelayWithIntialDelay() { log.info("fixedDelayWithIntialDelay"); }

Output is as follows:

2019-11-19 22:10:02,092 main:Tomcat started on port(s): 443 (http) with context path '' 2019-11-19 22:10:02,095 main:Started DemoApplication in 1.272 seconds (JVM running for 1.767) 2019-11-19 22:10:32,063 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:33,067 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:34,069 scheduling-1:fixedDelayWithIntialDelay 2019-11-19 22:10:35,069 scheduling-1:fixedDelayWithIntialDelay

It can be seen in about 30 seconds after the start of the project, began to perform the task.

2.4 Cron Expressions

Provided above functions and can not meet all the needs of scheduling regular tasks, such as the need to send text messages every month on the 1st, every Saturday for data analysis. Here Cron expression comes in handy.

The following example shows the number of seconds whenever 06 when executed. code show as below:

@Scheduled(cron = "6 * * ? * *")
public void cron() { log.info("cron"); }

The results are as follows:

2019-11-19 22:20:06,003 scheduling-1:cron 2019-11-19 22:21:06,004 scheduling-1:cron 2019-11-19 22:22:06,002 scheduling-1:cron 

Cron expression is very powerful, very rich information on the Internet, where not start speaking up.

3 parameters of

The previous example would die on the parameters written code, if a more flexible, in fact, can be configured with parameters. So we need to modify the parameters of time, without having to modify the code, compile and then deploy the package, directly modify configuration files.

code show as below:

@Scheduled(cron = "${pkslow.cron}")
public void cronWithConfig() { log.info("cronWithConfig"); }

In application.properties configured as follows:

pkslow.cron=* * * ? * *

Code executed once one second.

4 If she suddenly disappeared

Since the Spring of default Scheduler is single-threaded, so there will be a problem if a task execution stuck, it would not proceed with the down. Log on performance is suddenly disappeared. The probability of this happening is not small, such as the operation of the database deadlock, http request timeout to wait indefinitely, there is a deadlock and other causes.

When this happens, the command should jstack pid > pid.ThreadDump.txtget the current thread situation, then analyze whether it is stuck, stuck in which part, and then analyze specific code. Solved by providing a timeout or retry the like.

5 Conclusion

This paper describes the scheduled tasks comment Spring's @Scheduleduse, about the use and configure a variety of ways. It is very convenient and simple, for simple enough to cope with the task of timing.


Welcome to public concern number < pumpkin slow, said >, you will continue to update ...

file

More books, more sharing; and more writing, more than finishing.

Guess you like

Origin www.cnblogs.com/xichji/p/11909572.html