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

  介绍

使用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调度格式,第一位表示秒

控制台输出

,只,____ ,,,, _ ,,,,, __  _  _   ,/\ \/,___ # 39;_  __  _  _ (_) _  __  __  _  \, \ \, \   (,(,)\ ___  |, & # 39; _  |, & # 39; _ |, |, & # 39; _  \/, _”, | \ \祝福;\,\   ,\ \/___)|,| _)|,|,|,|,|,| |,(_ |,|,),),,)   & # 39;大敌;__ | |,.__ | _ |,| _ | _ |,| _ \ __,|,/,/,/,/,=========| _ |==============| ___/=/_/_/_/,::Spring  Boot ::,,,, (v2.1.0.RELEASE)      2018 - 12 - 12,15:01:00.332  INFO  34660,——安康;(,,,,,主要],bj.scheduler.SchedulerApp ,,,,,,,,, Starting  SchedulerApp 提醒macbook -空气- 2. - local  with  PID  34660,(/用户//temp/java/hellomaven刘宇超(音)/目标/classes  started  by  yuchao 拷贝/用户//temp/java/hellomaven)刘宇超(音)   2018 - 12 - 12,15:01:00.339  INFO  34660,——安康;(,,,,,主要],bj.scheduler.SchedulerApp ,,,,,,,,, No  active  profile ,, falling  back 用default 概要:违约   2018 - 12 - 12,15:01:02.395  INFO  34660,——安康;(,,,,,主要],o.s.s.c.ThreadPoolTaskScheduler ,,,,,, Initializing  ExecutorService  & # 39; taskScheduler& # 39;   2018 - 12 - 12,15:01:02.496  WARN  34660,——安康;(,,,,,主要],reactor.netty.tcp.TcpResources ,,,,,,, (http), resources  will  use 从而default  LoopResources:, DefaultLoopResources {前缀=reactor-http,守护进程=true,, selectCount=4, workerCount=4}   2018 - 12 - 12,15:01:02.498  WARN  34660,——安康;(,,,,,主要],reactor.netty.tcp.TcpResources ,,,,,,, (http), resources  will  use 从而default  ConnectionProvider:, PooledConnectionProvider  {name=http、, poolFactory=λreactor.netty.resources.ConnectionProvider $ $ $ 278/687399269@6594402a}   2018 - 12 - 12,15:01:02.707  INFO  34660,——安康;(安排一才能),bj.scheduler.SchedulerApp ,,,,,,,,, 2018 - 12 - 12 - t15:01:02.707 你好   2018 - 12 - 12,15:01:02.707  INFO  34660,——安康;(安排一才能),bj.scheduler.SchedulerApp ,,,,,,,,, 2018 - 12 - 12 - t15:01:02.707 你好   null   null   null   null   null   null   null   null   null   null

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