前言
項目常常用到定時任務钢属,在某個特定的時間欠啤,程序會自主觸發(fā)去執(zhí)行一些機械重復的工作熄阻,例如定時發(fā)送郵件膘掰、定時釋放數據庫資源跪另、定時票據核銷等拧抖。
一、cron表達式
對于cron表達式免绿,其結構是: 從左到右(用空格隔開):秒 分 小時 月份中的日期 月份 星期中的日期 年份
二唧席、注解配置
1.Scheduled
1.@Scheduled(cron = “0 10 * * * ?”)
cron表達式就不多說了
2.@Scheduled(fixedDelay = 10000)
fixedDelay 的執(zhí)行規(guī)則是上一個任務結束后到下個任務開始的間隔時間為設定時間,單位是毫秒(例:@Scheduled(fixedDelay = 10000)代表間隔10秒執(zhí)行一次)
3.@Scheduled(fixedRate= 10000)
fixedRate表示上個任務開始到下個任務開始之間的間隔時間,單位也是毫秒袱吆。
4.@Scheduled(initialDelay= 10000)
這個代表第一次運行前延遲一段時間后執(zhí)行厌衙,單位也是毫秒
2.使用注解實現定時任務
首先,啟動類上加上@EnableScheduling
新建定時任務測試類:
@Component
@Slf4j
public class TaskDemo {
@Scheduled(cron = "*/1 * * * * ?")
public void task1() {
log.info("task1:每1秒執(zhí)行一次 " +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
@Scheduled(cron = "*/2 * * * * ?")
public void task2() {
try {
log.info("task2:每2秒執(zhí)行一次 " +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執(zhí)行程序:
當有多個方法使用@Scheduled注解時绞绒,就會創(chuàng)建多個定時任務到任務列表中婶希,當其中一個任務沒執(zhí)行完時,其它任務在阻塞隊列當中等待蓬衡,因此喻杈,所有的任務都是按照順序執(zhí)行的。
這時需要用到@Async
實現多線程
@Component
@Slf4j
@EnableAsync
public class TaskDemo {
@Async
@Scheduled(cron = "*/1 * * * * ?")
public void task1() {
log.info("task1:每1秒執(zhí)行一次 " +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
@Async
@Scheduled(cron = "*/2 * * * * ?")
public void task2() {
try {
log.info("task2:每2秒執(zhí)行一次 " +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
運行結果:
三狰晚、xml配置實現
1.線程池配置
新建springmvc-servlet.xml進行配置
<!-- 線程池 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心線程數 -->
<property name="corePoolSize" value="5" />
<!-- 最大線程數 -->
<property name="maxPoolSize" value="50" />
<!-- 隊列最大長度 -->
<property name="queueCapacity" value="1000" />
<!-- 線程池維護線程所允許的空閑時間筒饰,默認為60s -->
<property name="keepAliveSeconds" value="60" />
</bean>
2.任務配置
spring中使用task:annotation-driven標簽作為定時器的啟動開關,自動掃描程序中帶注解的定時器壁晒。
<!-- 任務調度器線程數量 -->
<task:scheduler id="scheduler" pool-size="5"/>
<task:annotation-driven scheduler="scheduler" executor="taskExecutor" proxy-target-class="true"/>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="taskDemo" method="task1" cron="*/1 * * * * ?"/>
<task:scheduled ref="taskDemo" method="task2" cron="*/2 * * * * ?"/>
</task:scheduled-tasks>
3.引入外部xml
貼出完整的xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.local.dev.root.devroot.common.task"/>
<!-- <bean id="taskDemo" class="com.local.dev.root.devroot.common.task.TaskDemo"></bean>-->
<!-- 線程池 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心線程數 -->
<property name="corePoolSize" value="5"/>
<!-- 最大線程數 -->
<property name="maxPoolSize" value="50"/>
<!-- 隊列最大長度 -->
<property name="queueCapacity" value="1000"/>
<!-- 線程池維護線程所允許的空閑時間瓷们,默認為60s -->
<property name="keepAliveSeconds" value="60"/>
</bean>
<!-- 配置任務線性池 -->
<!-- 任務執(zhí)行器線程數量 -->
<!--<task:executor id="executor" pool-size="3" />-->
<!-- 任務調度器線程數量 -->
<task:scheduler id="scheduler" pool-size="5"/>
<task:annotation-driven scheduler="scheduler" executor="taskExecutor" proxy-target-class="true"/>
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="taskDemo" method="task1" cron="*/1 * * * * ?"/>
<task:scheduled ref="taskDemo" method="task2" cron="*/2 * * * * ?"/>
</task:scheduled-tasks>
</beans>
在啟動類加上@ImportResource("classpath:spring-servlet.xml")
執(zhí)行程序:
總結
以上就是今天要講的內容,本文僅僅簡單介紹了定時器的使用秒咐,注解實現很簡單谬晕,但是項目中推薦使用xml配置的方式。
Springboot系列就暫時到這携取,后續(xù)再更新
? 上一章:SpringBoot —— 實現郵件攒钳、短信的發(fā)送功能
? 下一章:SpringBoot ——統(tǒng)一異常處理