spring配置定時任務(wù)的幾種方式總結(jié)
網(wǎng)上看到好多關(guān)于定時任務(wù)的講解,以前只簡單使用過注解方式,今天項目中看到基于配置的方式實現(xiàn)定時任務(wù),自己做個總結(jié),作為備忘錄吧。
基于注解方式的定時任務(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" />
最后貼上定時任務(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("----定時任務(wù)開始執(zhí)行-----");
//執(zhí)行具體業(yè)務(wù)邏輯----------
System.out.println("----定時任務(wù)執(zhí)行結(jié)束-----");
}
}基于配置的定時任務(wù)調(diào)度框架Quartz
引入依賴
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
定義一個類,方法可以寫多個為需要定時執(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í)行的定時任務(wù)方法名-->
<bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminJob"/>
<property name="targetMethod" value="job1"/>
</bean>
<!--此處為定時任務(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的定時任務(wù)代碼
基于線程池的方式實現(xiàn)定時任務(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot定時任務(wù)的實現(xiàn)詳解
- 利用SpringBoot解決多個定時任務(wù)阻塞的問題
- springboot中設(shè)置定時任務(wù)的三種方法小結(jié)
- Spring定時任務(wù)@scheduled多線程使用@Async注解示例
- Spring定時任務(wù)@Scheduled注解(cron表達式fixedRate?fixedDelay)
- SpringBoot中實現(xiàn)定時任務(wù)的4種方式詳解
- Spring中的Schedule動態(tài)添加修改定時任務(wù)詳解
- SpringBoot中的定時任務(wù)和異步調(diào)用詳解
- SpringBoot實現(xiàn)設(shè)置動態(tài)定時任務(wù)的方法詳解
- Spring定時任務(wù)注解@Scheduled詳解
- spring動態(tài)控制定時任務(wù)的實現(xiàn)
相關(guān)文章
使用Spring Security集成手機驗證碼登錄功能實現(xiàn)
本文詳細介紹了如何利用SpringSecurity來實現(xiàn)手機驗證碼的注冊和登錄功能,在登錄過程中,同樣需通過驗證碼進行驗證,文章還提供了相關(guān)的代碼實現(xiàn)2024-10-10
springboot 多環(huán)境配置 yml文件版的實現(xiàn)方法
這篇文章主要介紹了springboot 多環(huán)境配置 yml文件版的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
淺析Java設(shè)計模式編程中的單例模式和簡單工廠模式
這篇文章主要介紹了淺析Java設(shè)計模式編程中的單例模式和簡單工廠模式,使用設(shè)計模式編寫代碼有利于團隊協(xié)作時程序的維護,需要的朋友可以參考下2016-01-01
SpringBoot自動配置@EnableAutoConfiguration過程示例
這篇文章主要為大家介紹了SpringBoot自動配置@EnableAutoConfiguration的過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

