spring配置定時(shí)任務(wù)的幾種方式總結(jié)
網(wǎng)上看到好多關(guān)于定時(shí)任務(wù)的講解,以前只簡單使用過注解方式,今天項(xiàng)目中看到基于配置的方式實(shí)現(xiàn)定時(shí)任務(wù),自己做個總結(jié),作為備忘錄吧。
基于注解方式的定時(shí)任務(wù)
首先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
其次需要配置注解驅(qū)動
<task:annotation-driven />
添加你添加注解的掃描包
<context:component-scan base-package="com.xxx.xxx" />
最后貼上定時(shí)任務(wù)包代碼
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("----定時(shí)任務(wù)開始執(zhí)行-----");
//執(zhí)行具體業(yè)務(wù)邏輯----------
System.out.println("----定時(shí)任務(wù)執(zhí)行結(jié)束-----");
}
}基于配置的定時(shí)任務(wù)調(diào)度框架Quartz
引入依賴
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
定義一個類,方法可以寫多個為需要定時(shí)執(zhí)行的任務(wù)
public class AdminJob {
public void job1() {
System.out.pringln("執(zhí)行了任務(wù)---");
}
}在spring.xml配置中添加
<bean id="adminJob" class="com.xxx.xxx.AdminJob"/>
<!--此處id值為需要執(zhí)行的定時(shí)任務(wù)方法名-->
<bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminJob"/>
<property name="targetMethod" value="job1"/>
</bean>
<!--此處為定時(shí)任務(wù)觸發(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的定時(shí)任務(wù)代碼
基于線程池的方式實(shí)現(xiàn)定時(shí)任務(wù)
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);
}
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot定時(shí)任務(wù)的實(shí)現(xiàn)詳解
- 利用SpringBoot解決多個定時(shí)任務(wù)阻塞的問題
- springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)
- Spring定時(shí)任務(wù)@scheduled多線程使用@Async注解示例
- Spring定時(shí)任務(wù)@Scheduled注解(cron表達(dá)式fixedRate?fixedDelay)
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的4種方式詳解
- Spring中的Schedule動態(tài)添加修改定時(shí)任務(wù)詳解
- SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解
- SpringBoot實(shí)現(xiàn)設(shè)置動態(tài)定時(shí)任務(wù)的方法詳解
- Spring定時(shí)任務(wù)注解@Scheduled詳解
- spring動態(tài)控制定時(shí)任務(wù)的實(shí)現(xiàn)
相關(guān)文章
java數(shù)學(xué)歸納法非遞歸求斐波那契數(shù)列的方法
這篇文章主要介紹了java數(shù)學(xué)歸納法非遞歸求斐波那契數(shù)列的方法,涉及java非遞歸算法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Spring?Cloud?中使用?Sentinel?實(shí)現(xiàn)服務(wù)限流的兩種方式
這篇文章主要介紹了Spring?Cloud?中使用?Sentinel?實(shí)現(xiàn)服務(wù)限流的方式,通過示例代碼主要介紹了Sentinel的兩種實(shí)現(xiàn)限流的方式,需要的朋友可以參考下2024-03-03
Spring?Boot項(xiàng)目Jar包加密實(shí)戰(zhàn)教程
本文詳細(xì)介紹了如何在Spring?Boot項(xiàng)目中實(shí)現(xiàn)Jar包加密,我們首先了解了Jar包加密的基本概念和作用,然后學(xué)習(xí)了如何使用Spring?Boot的Jar工具和第三方庫來實(shí)現(xiàn)Jar包的加密和解密,感興趣的朋友一起看看吧2024-02-02
SpringBoot實(shí)現(xiàn)Server-Sent Events(SSE)的使用完整指南
使用SpringBoot實(shí)現(xiàn)Server-Sent Events(SSE)可以有效處理實(shí)時(shí)數(shù)據(jù)推送需求,具有單向通信、輕量級和高實(shí)時(shí)性等優(yōu)勢,本文詳細(xì)介紹了在SpringBoot中創(chuàng)建SSE端點(diǎn)的步驟,并通過代碼示例展示了客戶端如何接收數(shù)據(jù),適用于實(shí)時(shí)通知、數(shù)據(jù)展示和在線聊天等場景2024-09-09

