spring配置定時(shí)任務(wù)的幾種方式總結(jié)
網(wǎng)上看到好多關(guān)于定時(shí)任務(wù)的講解,以前只簡(jiǎn)單使用過(guò)注解方式,今天項(xiàng)目中看到基于配置的方式實(shí)現(xiàn)定時(shí)任務(wù),自己做個(gè)總結(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ū)動(dòng)
<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ù)開(kāi)始執(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>
定義一個(gè)類,方法可以寫多個(gè)為需要定時(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot定時(shí)任務(wù)的實(shí)現(xiàn)詳解
- 利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題
- 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動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解
- SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解
- SpringBoot實(shí)現(xiàn)設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法詳解
- Spring定時(shí)任務(wù)注解@Scheduled詳解
- spring動(dòng)態(tài)控制定時(shí)任務(wù)的實(shí)現(xiàn)
相關(guān)文章
使用Spring Security集成手機(jī)驗(yàn)證碼登錄功能實(shí)現(xiàn)
本文詳細(xì)介紹了如何利用SpringSecurity來(lái)實(shí)現(xiàn)手機(jī)驗(yàn)證碼的注冊(cè)和登錄功能,在登錄過(guò)程中,同樣需通過(guò)驗(yàn)證碼進(jìn)行驗(yàn)證,文章還提供了相關(guān)的代碼實(shí)現(xiàn)2024-10-10
Spring+MongoDB實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Spring+MongoDB實(shí)現(xiàn)登錄注冊(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
JVM中對(duì)象的創(chuàng)建與OOP-Klass模型
這篇文章主要介紹了JVM中對(duì)象的創(chuàng)建與OOP-Klass模型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 多環(huán)境配置 yml文件版的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式
這篇文章主要介紹了淺析Java設(shè)計(jì)模式編程中的單例模式和簡(jiǎn)單工廠模式,使用設(shè)計(jì)模式編寫代碼有利于團(tuán)隊(duì)協(xié)作時(shí)程序的維護(hù),需要的朋友可以參考下2016-01-01
SpringBoot自動(dòng)配置@EnableAutoConfiguration過(guò)程示例
這篇文章主要為大家介紹了SpringBoot自動(dòng)配置@EnableAutoConfiguration的過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

