首页| 新闻| 娱乐| 游戏| 科普| 文学| 编程| 系统| 数据库| 建站| 学院| 产品| 网管| 维修| 办公| 热点
SpringBoot自带了任务调度器,通过注解的方式使用。
启用方式: 在配置类上注解 org.springframework.scheduling.annotation.EnableScheduling
org.springframework.scheduling.annotation.EnableScheduling
Java示例
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@Slf4jpublic 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()); }}
要点
控制台输出
. ____ _ __ _ _ /// / ___'_ __ _ _(_)_ __ __ _ / / / /( ( )/___ | '_ | '_| | '_ // _` | / / / / /// ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_/__, | / / / / =========|_|==============|___/=/_/_/_/ :: 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: default2018-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 Hello2018-12-12 15:01:02.707 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:02.707 Hello2018-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 Hello2018-12-12 15:01:03.704 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:03.704 Hello2018-12-12 15:01:03.710 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:03.710 Hello2018-12-12 15:01:04.002 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:04.002 Hello2018-12-12 15:01:04.702 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:04.702 Hello2018-12-12 15:01:04.712 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:04.712 Hello2018-12-12 15:01:05.000 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:05 Hello2018-12-12 15:01:05.700 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:05.700 Hello2018-12-12 15:01:05.716 INFO 34660 --- [ scheduling-1] bj.scheduler.SchedulerApp : 2018-12-12T15:01:05.716 Hello
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。
练就火眼金睛 十一种常见电脑
打印机共享提示“操作无法完成
如何查找有故障的配件
回眸一笑百魅生,六宫粉黛无颜色
岁月静美,剪一影烟雨江南
芜湖有个“松鼠小镇”
小满:小得盈满,一切刚刚好!
一串串晶莹剔透的葡萄,像一颗颗宝石挂在藤
正宗老北京脆皮烤鸭
人逢知己千杯少,喝酒搞笑图集
搞笑试卷,学生恶搞答题
新闻热点
疑难解答
图片精选
Kotlin结合Rxjava+Retrofit实现极
Kotlin与Java的主客观对比分析
Kotlin特性介绍及与Java 和 C#的简
Kotlin与Java哪个好?Kotlin与Java的
网友关注