服务器之家

服务器之家 > 正文

SpringBoot任务调度器的实现代码

时间:2021-06-22 12:50     来源/作者:白季飞龙

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

启用方式: 在配置类上注解 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://baijifeilong.github.io/2018/12/12/spring-boot-scheduler/

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部