spring配置定時任務的幾種方式總結
更新時間:2023年12月22日 10:35:38 作者:螞蟻style
這篇文章主要介紹了spring配置定時任務的幾種方式總結,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
網上看到好多關于定時任務的講解,以前只簡單使用過注解方式,今天項目中看到基于配置的方式實現定時任務,自己做個總結,作為備忘錄吧。
基于注解方式的定時任務
首先spring-mvc.xml的配置文件中添加約束文件
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
其次需要配置注解驅動
<task:annotation-driven />
添加你添加注解的掃描包
<context:component-scan base-package="com.xxx.xxx" />
最后貼上定時任務包代碼
package com.xxx.xxx;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class xxxTask {
@Scheduled(cron = "0/5 * * * * ? ") // 間隔5秒執(zhí)行
public void xxx() {
System.out.println("----定時任務開始執(zhí)行-----");
//執(zhí)行具體業(yè)務邏輯----------
System.out.println("----定時任務執(zhí)行結束-----");
}
}基于配置的定時任務調度框架Quartz
引入依賴
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
定義一個類,方法可以寫多個為需要定時執(zhí)行的任務
public class AdminJob {
public void job1() {
System.out.pringln("執(zhí)行了任務---");
}
}在spring.xml配置中添加
<bean id="adminJob" class="com.xxx.xxx.AdminJob"/>
<!--此處id值為需要執(zhí)行的定時任務方法名-->
<bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminJob"/>
<property name="targetMethod" value="job1"/>
</bean>
<!--此處為定時任務觸發(fā)器-->
<bean id="job1Trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="job1"/>
</property>
<property name="cronExpression">
<value>0 15 0 16 * ?</value>
</property>
</bean><bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="job1Trigger"/>
</list>
</property>
<property name="taskExecutor" ref="executor"/>
</bean>
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10"/>
<property name="maxPoolSize" value="100"/>
<property name="queueCapacity" value="500"/>
</bean>最后還有一種普通java的定時任務代碼
基于線程池的方式實現定時任務
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot定時任務的實現詳解
- 利用SpringBoot解決多個定時任務阻塞的問題
- springboot中設置定時任務的三種方法小結
- Spring定時任務@scheduled多線程使用@Async注解示例
- Spring定時任務@Scheduled注解(cron表達式fixedRate?fixedDelay)
- SpringBoot中實現定時任務的4種方式詳解
- Spring中的Schedule動態(tài)添加修改定時任務詳解
- SpringBoot中的定時任務和異步調用詳解
- SpringBoot實現設置動態(tài)定時任務的方法詳解
- Spring定時任務注解@Scheduled詳解
- spring動態(tài)控制定時任務的實現
相關文章
Spring?Cloud?中使用?Sentinel?實現服務限流的兩種方式
這篇文章主要介紹了Spring?Cloud?中使用?Sentinel?實現服務限流的方式,通過示例代碼主要介紹了Sentinel的兩種實現限流的方式,需要的朋友可以參考下2024-03-03
SpringBoot實現Server-Sent Events(SSE)的使用完整指南
使用SpringBoot實現Server-Sent Events(SSE)可以有效處理實時數據推送需求,具有單向通信、輕量級和高實時性等優(yōu)勢,本文詳細介紹了在SpringBoot中創(chuàng)建SSE端點的步驟,并通過代碼示例展示了客戶端如何接收數據,適用于實時通知、數據展示和在線聊天等場景2024-09-09

