SpringTask實(shí)現(xiàn)定時(shí)任務(wù)方法講解
SpringTask是Spring自帶的功能。實(shí)現(xiàn)起來比較簡(jiǎn)單。
使用SpringTask實(shí)現(xiàn)定時(shí)任務(wù)有兩種方式:
1.注解方式
基于注解@Scheduled
@Scheduled(cron = "*/1 * * * * ?") public void up(){ System.out.println("定時(shí)任務(wù)開啟:"+System.currentTimeMillis()); }
cron表達(dá)式定義定時(shí)任務(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"> <!--聲明一個(gè)具有一個(gè)線程的池,如果定義多個(gè),每個(gè)對(duì)象將獲取同樣的運(yùn)行機(jī)會(huì)--> <task:scheduler id="sch" pool-size="10"/> <!--任務(wù)的調(diào)度類--> <bean id="scheduleTask" class="com.cloudtop.base.task.ScheduleTask"/> <!--引用線程池--> <task:scheduled-tasks scheduler="sch"> <!--年報(bào)調(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)度類實(shí)現(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; /** * 定時(shí)任務(wù)類 */ public class ScheduleTask { @Autowired EnvironmentUpService environmentUpService; public void yearReportTask() throws BusinessException { System.out.println("*******定時(shí)任務(wù)執(zhí)行的業(yè)務(wù)代碼******"); } }
最后,第一種使用注解@EnableSchedu ling開啟定時(shí)任務(wù),第二種使用xml的方式配置好上面的三個(gè)文件就開啟了定時(shí)任務(wù),不用使用注解@EnableSchedu ling來開啟定時(shí)任務(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項(xiàng)目均采用main方法啟動(dòng)主程序,該部分為必須項(xiàng) * @param args */ public static void main(String[] args) { SpringApplication.run(CloudtopWebFrameApplication.class, args); } }
最后在控制臺(tái)會(huì)輸出結(jié)果:
到此這篇關(guān)于SpringTask實(shí)現(xiàn)定時(shí)任務(wù)方法講解的文章就介紹到這了,更多相關(guān)SpringTask定時(shí)任務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis-plus使用@EnumValue處理枚舉類型的示例代碼
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09詳談java中File類getPath()、getAbsolutePath()、getCanonical的區(qū)別
下面小編就為大家?guī)硪黄斦刯ava中File類getPath()、getAbsolutePath()、getCanonical的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07設(shè)計(jì)模式之責(zé)任鏈模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了設(shè)計(jì)模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Java?事務(wù)注解@Transactional回滾(try?catch、嵌套)問題
這篇文章主要介紹了Java?@Transactional回滾(try?catch、嵌套)問題,Spring?事務(wù)注解?@Transactional?本來可以保證原子性,如果事務(wù)內(nèi)有報(bào)錯(cuò)的話,整個(gè)事務(wù)可以保證回滾,但是加上try?catch或者事務(wù)嵌套,可能會(huì)導(dǎo)致事務(wù)回滾失敗2022-08-08java讀取文件:char的ASCII碼值=65279,顯示是一個(gè)空字符的解決
這篇文章主要介紹了java讀取文件:char的ASCII碼值=65279,顯示是一個(gè)空字符的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08淺談Spring Cloud Netflix-Ribbon灰度方案之Zuul網(wǎng)關(guān)灰度
這篇文章主要介紹了淺談Spring Cloud Netflix-Ribbon灰度方案之Zuul網(wǎng)關(guān)灰度,想了解Ribbon灰度的同學(xué)可以參考下2021-04-04