SpringTask實現(xiàn)定時任務(wù)方法講解
SpringTask是Spring自帶的功能。實現(xiàn)起來比較簡單。
使用SpringTask實現(xiàn)定時任務(wù)有兩種方式:
1.注解方式
基于注解@Scheduled
@Scheduled(cron = "*/1 * * * * ?")
public void up(){
System.out.println("定時任務(wù)開啟:"+System.currentTimeMillis());
}cron表達式定義定時任務(wù)如何去執(zhí)行。
2.配置文件xml方式
基于xml的方式【@Configuration + @ImportResource + xml】需要重啟應(yīng)用才能生效
配置xml文件,定義xml文件的名稱為task.xml,放置文件在resources文件夾下:

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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--聲明一個具有一個線程的池,如果定義多個,每個對象將獲取同樣的運行機會-->
<task:scheduler id="sch" pool-size="10"/>
<!--任務(wù)的調(diào)度類-->
<bean id="scheduleTask" class="com.cloudtop.base.task.ScheduleTask"/>
<!--引用線程池-->
<task:scheduled-tasks scheduler="sch">
<!--年報調(diào)度任務(wù) 5秒-->
<task:scheduled ref="scheduleTask" method="yearReportTask" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>配置類加載xml文件
package com.cloudtop.base.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* 加載調(diào)度的配置文件
*/
@Configuration
@ImportResource(locations={"classpath:task/task.xml"})//加載調(diào)度xml
public class SpringTaskConfig {
}任務(wù)的調(diào)度類實現(xiàn)
package com.cloudtop.base.task;
import com.cloudtop.base.error.exception.BusinessException;
import com.cloudtop.core.service.EnvironmentUpService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 定時任務(wù)類
*/
public class ScheduleTask {
@Autowired
EnvironmentUpService environmentUpService;
public void yearReportTask() throws BusinessException {
System.out.println("*******定時任務(wù)執(zhí)行的業(yè)務(wù)代碼******");
}
}最后,第一種使用注解@EnableSchedu ling開啟定時任務(wù),第二種使用xml的方式配置好上面的三個文件就開啟了定時任務(wù),不用使用注解@EnableSchedu ling來開啟定時任務(wù)。
@SpringBootApplication
@ServletComponentScan
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
@EnableSchedu ling
public class CloudtopWebFrameApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(CloudtopWebFrameApplication.class);
}
/**
* 主程序入口
* 所有SpringBoot項目均采用main方法啟動主程序,該部分為必須項
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(CloudtopWebFrameApplication.class, args);
}
}最后在控制臺會輸出結(jié)果:

到此這篇關(guān)于SpringTask實現(xiàn)定時任務(wù)方法講解的文章就介紹到這了,更多相關(guān)SpringTask定時任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)File轉(zhuǎn)換MultipartFile格式的例子
本文主要介紹了Java實現(xiàn)File轉(zhuǎn)換MultipartFile格式的例子,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot Actuator未授權(quán)訪問漏洞解決方案
工作的時候遇到過提示Spring Boot后端存在Actuator未授權(quán)訪問漏洞,網(wǎng)上有很多詳細的解釋文章,在這里做一個簡單的總結(jié)、介紹和分享,需要的朋友可以參考下2023-09-09
Java刪除指定文件夾下的所有內(nèi)容的方法(包括此文件夾)
下面小編就為大家?guī)硪黄狫ava刪除指定文件夾下的所有內(nèi)容的方法(包括此文件夾) 。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Java使用Collections工具類對List集合進行排序
這篇文章主要介紹了Java使用Collections工具類對List集合進行排序,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10

