启用方式: 在配置类上注解 org.springframework.scheduling.annotation.enablescheduling
java示例
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package bj.scheduler;
import lombok.extern.slf4j.slf4j;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;
import org.springframework.scheduling.annotation.enablescheduling;
import org.springframework.scheduling.annotation.scheduled;
import org.springframework.scheduling.annotation.schedules;
import java.time.localdatetime;
/**
* created by baijifeilong@gmail.com at 2018/12/12 下午2:51
*/
@springbootapplication (exclude = datasourceautoconfiguration. class )
@enablescheduling
@slf4j
public class schedulerapp {
public static void main(string[] args) throws interruptedexception {
springapplication.run(schedulerapp. class , args);
thread.currentthread().join();
}
@schedules ({
@scheduled (fixedrate = 1000 ),
@scheduled (fixeddelay = 1001 ),
@scheduled (cron = "* * * * * *" )
})
public void sayhello() {
log.info( "{} hello" , localdatetime.now());
}
}
|
要点
- @enablescheduling 启用任务调度器
- @schedules 组合多个调度器。多个调度器全部启用。
- @scheduled 单个调度器的配置
- fixedrate 固定执行频率(毫秒),不计执行耗时
- fixeddelay 固定执行延迟(毫秒),表示距离上次执行完毕的时长
- cron crontab调度格式,第一位表示秒
控制台输出
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
. ____ _ __ _ _
/\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\
( ( )\\___ | '_ | ' _| | '_ \\/ _` | \\ \\ \\ \\
\\\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: spring boot :: (v2. 1.0 .release)
2018 - 12 - 12 15 : 01 : 00.332 info 34660 --- [ main] bj.scheduler.schedulerapp : starting schedulerapp on macbook-air- 2 .local with pid 34660 (/users/yuchao/temp/java/hellomaven/target/classes started by yuchao in /users/yuchao/temp/java/hellomaven)
2018 - 12 - 12 15 : 01 : 00.339 info 34660 --- [ main] bj.scheduler.schedulerapp : no active profile set, falling back to default profiles: default
2018 - 12 - 12 15 : 01 : 02.395 info 34660 --- [ main] o.s.s.c.threadpooltaskscheduler : initializing executorservice 'taskscheduler'
2018 - 12 - 12 15 : 01 : 02.496 warn 34660 --- [ main] reactor.netty.tcp.tcpresources : [http] resources will use the default loopresources: defaultloopresources {prefix=reactor-http, daemon= true , selectcount= 4 , workercount= 4 }
2018 - 12 - 12 15 : 01 : 02.498 warn 34660 --- [ main] reactor.netty.tcp.tcpresources : [http] resources will use the default connectionprovider: pooledconnectionprovider {name=http, poolfactory=reactor.netty.resources.connectionprovider$$lambda$ 278 / 687399269 @6594402a }
2018 - 12 - 12 15 : 01 : 02.707 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 02.707 hello
2018 - 12 - 12 15 : 01 : 02.707 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 02.707 hello
2018 - 12 - 12 15 : 01 : 02.708 info 34660 --- [ main] bj.scheduler.schedulerapp : started schedulerapp in 3.257 seconds (jvm running for 4.997 )
2018 - 12 - 12 15 : 01 : 03.004 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 03.004 hello
2018 - 12 - 12 15 : 01 : 03.704 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 03.704 hello
2018 - 12 - 12 15 : 01 : 03.710 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 03.710 hello
2018 - 12 - 12 15 : 01 : 04.002 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 04.002 hello
2018 - 12 - 12 15 : 01 : 04.702 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 04.702 hello
2018 - 12 - 12 15 : 01 : 04.712 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 04.712 hello
2018 - 12 - 12 15 : 01 : 05.000 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 05 hello
2018 - 12 - 12 15 : 01 : 05.700 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 05.700 hello
2018 - 12 - 12 15 : 01 : 05.716 info 34660 --- [ scheduling- 1 ] bj.scheduler.schedulerapp : 2018 - 12 -12t15: 01 : 05.716 hello
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://baijifeilong.github.io/2018/12/12/spring-boot-scheduler/
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 71
-
2025-05-29 29
-
2025-05-29 89
-
2025-06-04 78
-
2025-05-25 54
热门评论