Java spring定時任務詳解
一、定時任務
1、cron表達式
語法:秒 分 時 日 月 周 年
(其中“年”Spring不支持,也就是說在spring定時任務中只能設置:秒 分 時 日 月 周)
2、cron示例
3、SpringBoot整合
@EnableScheduling
@Scheduled
實例:
package com.xunqi.gulimall.seckill.scheduled; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * @Description: * @Created: with IntelliJ IDEA. * @author: 夏沫止水 * @createTime: 2020-07-09 18:49 **/ /** * 定時任務 * 1、@EnableScheduling 開啟定時任務 * 2、@Scheduled開啟一個定時任務 * * 異步任務 * 1、@EnableAsync:開啟異步任務 * 2、@Async:給希望異步執(zhí)行的方法標注 */ @Slf4j @Component @EnableScheduling public class HelloScheduled { /** * 1、在Spring中表達式是6位組成,不允許第七位的年份 * 2、在周幾的的位置,1-7代表周一到周日 * 3、定時任務不該阻塞。默認是阻塞的 * 1)、可以讓業(yè)務以異步的方式,自己提交到線程池 * CompletableFuture.runAsync(() -> { * },execute); * * 2)、支持定時任務線程池;設置 TaskSchedulingProperties * spring.task.scheduling.pool.size: 5 * * 3)、讓定時任務異步執(zhí)行 * 異步任務 * * 解決:使用異步任務 + 定時任務來完成定時任務不阻塞的功能 * */ @Scheduled(cron = "*/1 * * * * ?") public void hello() { log.info("hello..."); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } }
定時任務默認是阻塞的線程,也就是說即使你設置成每一秒執(zhí)行一次,但是方法內(nèi)部的業(yè)務時間需要5秒才能執(zhí)行完,也會造成定時任務每6秒才能執(zhí)行一次。
當然我們可以開啟異步線程:
@EnableAsync
@Async
實例:
package com.xunqi.gulimall.seckill.scheduled; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * @Description: * @Created: with IntelliJ IDEA. * @author: 夏沫止水 * @createTime: 2020-07-09 18:49 **/ /** * 定時任務 * 1、@EnableScheduling 開啟定時任務 * 2、@Scheduled開啟一個定時任務 * * 異步任務 * 1、@EnableAsync:開啟異步任務 * 2、@Async:給希望異步執(zhí)行的方法標注 */ @Slf4j @Component @EnableAsync @EnableScheduling public class HelloScheduled { /** * 1、在Spring中表達式是6位組成,不允許第七位的年份 * 2、在周幾的的位置,1-7代表周一到周日 * 3、定時任務不該阻塞。默認是阻塞的 * 1)、可以讓業(yè)務以異步的方式,自己提交到線程池 * CompletableFuture.runAsync(() -> { * },execute); * * 2)、支持定時任務線程池;設置 TaskSchedulingProperties * spring.task.scheduling.pool.size: 5 * * 3)、讓定時任務異步執(zhí)行 * 異步任務 * * 解決:使用異步任務 + 定時任務來完成定時任務不阻塞的功能 * */ @Async @Scheduled(cron = "*/1 * * * * ?") public void hello() { log.info("hello..."); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } }
這樣就會開啟異步線程,并且是非阻塞線程,因為每次都會開啟一個線程來執(zhí)行,我們可以看一下源碼配置的截圖,這個就是異步執(zhí)行的默認配置,核心線程數(shù)是8,最大線程數(shù)是無限大,這時如果一直每秒執(zhí)行一次,則會造成服務器資源耗盡。
當然,我們可以在配置文件中進行定時任務線程池的設定:
#核心線程數(shù)
spring.task.execution.pool.core-size=20
#最大線程數(shù)
spring.task.execution.pool.max-size=50
#隊列大小
spring.task.execution.pool.queue-capacity=10000
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
如何解決Maven出現(xiàn)Could not find artifact的問題
這篇文章主要介紹了如何解決Maven出現(xiàn)Could not find artifact的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04SpringBoot整合SpringSession實現(xiàn)分布式登錄詳情
這篇文章主要介紹了SpringBoot整合SpringSession實現(xiàn)分布式登錄詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08在spring?boot3中使用native?image的最新方法
這篇文章主要介紹了在spring?boot3中使用native?image?,今天我們用具體的例子來給大家演示一下如何正確的將spring boot3的應用編譯成為native image,需要的朋友可以參考下2023-01-01Java簡單實現(xiàn)session保存到redis的方法示例
這篇文章主要介紹了Java簡單實現(xiàn)session保存到redis的方法,結(jié)合實例形式分析了Java將session存入redis緩存服務器的相關設置、實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2018-05-05