SpringBoot任务调度器

栏目: Java · 发布时间: 6年前

内容简介:SpringBoot自带了任务调度器,通过注解的方式使用。启用方式: 在配置类上注解文章首发:

SpringBoot自带了任务调度器,通过注解的方式使用。

启用方式: 在配置类上注解 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
@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调度格式,第一位表示秒

控制台输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

文章首发: https://baijifeilong.github.io


以上所述就是小编给大家介绍的《SpringBoot任务调度器》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据挖掘导论

数据挖掘导论

Pang-Ning Tan, Michael Steinbach / 范明 范宏建 / 人民邮电出版社 / 2006-01-01 / 49.00元

本书全面介绍了数据挖掘的理论和方法,旨在为读者提供将数据挖掘应用于实际问题所必需的知识。本书涵盖五个主题:数据、分类、关联分析、聚类和异常检测。除异常检测外,每个主题都包含两章:前面一章讲述基本概念、代表性算法和评估技术,后面一章较深入地讨论高级概念和算法。目的是使读者在透彻地理解数据挖掘基础的同时,还能了解更多重要的高级主题。此外,书中还提供了大量示例、图表和习题。 本书适合作为......一起来看看 《数据挖掘导论》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具