使用SpringBoot怎么實現(xiàn)一個任務(wù)調(diào)度器?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
鎮(zhèn)沅ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
SpringBoot自帶了任務(wù)調(diào)度器,通過注解的方式使用。
啟用方式: 在配置類上注解 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 啟用任務(wù)調(diào)度器
@Schedules 組合多個調(diào)度器。多個調(diào)度器全部啟用。
@Scheduled 單個調(diào)度器的配置
fixedRate 固定執(zhí)行頻率(毫秒),不計執(zhí)行耗時
fixedDelay 固定執(zhí)行延遲(毫秒),表示距離上次執(zhí)行完畢的時長
cron CronTab調(diào)度格式,第一位表示秒
控制臺輸出
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: 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一種全新的編程規(guī)范,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
關(guān)于使用SpringBoot怎么實現(xiàn)一個任務(wù)調(diào)度器問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。