温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用SpringBoot怎么实现一个任务调度器

发布时间:2021-05-14 18:05:29 来源:亿速云 阅读:372 作者:Leah 栏目:编程语言

使用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;


@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

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

关于使用SpringBoot怎么实现一个任务调度器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI