SpringBoot任务调度器的实现代码
程序员文章站
2024-03-04 11:47:53
springboot自带了任务调度器,通过注解的方式使用。
启用方式: 在配置类上注解 org.springframework.scheduling.annotation...
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。