spring基於task標籤和通過xml配置實現任務的區別
我們可以簡單的通過以下配置:
匯入maven座標:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency>
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:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <context:component-scan base-package="com.xls" /> <!-- 開啟@AspectJ AOP代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 任務排程器 --> <task:scheduler id="scheduler" pool-size="10" /> <!-- 任務執行器 --> <task:executor id="executor" pool-size="10" /> <!--開啟註解排程支援 @Async @Scheduled --> <task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true" /> </beans>
java程式碼:
/** * */ package com.xls.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * * * @Description: * @Author: XiongLiangSheng * @CreateDate: 2014年9月28日 下午2:09:50 * @Version: v1.0 * * Date CR/DEFECT Modified By Description of change */ @Component public class SpringTaskDemo { @Scheduled(fixedDelay = 5000) void doSomethingWithDelay(){ System.out.println("I'm doing with delay now!"); } @Scheduled(fixedRate = 5000) void doSomethingWithRate(){ System.out.println("I'm doing with rate now!"); } @Scheduled(cron = "0/5 * * * * *") void doSomethingWith(){ System.out.println("I'm doing with cron now!"); } }
就可以定義好定時任務。
測試類
java程式碼:
/**
*
*/
package com.xls.task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
*
* @Description:
* @Author: XiongLiangSheng
* @CreateDate: 2014年9月28日 下午2:29:22
* @Version: v1.0
*
* Date CR/DEFECT Modified By Description of change
*/
public class TestTask {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:quartz.xml");
}
}
執行測試類我們可以看到定時任務已經在運行了。但是如果在配置檔案中加上default-lazy-init="true",測試類主執行緒會立即結束,似乎沒有發現有定時任務Scheduler。但是如果通過xml檔案配置quartz任務則不會有這種現象,但專案必須引入quartz相關的jar包,附件是兩種方式實現的定時任務。其區別有待查證。 轉:
Spring3.0以後,自己已經完全支援更加精確的時間,而不需要Quartz(Quartz是一個開放原始碼專案,專注於任務排程器,提供了極為廣泛的特性如持久化任務,叢集和分散式任務等。Spring對Quartz的整合與其對JDK Timer的整合在任務、觸發器和排程計劃的宣告式配置方面等都非常相似。 )的支援:當然後面我們也會用Quartz實現任務的排程。
Spring3.0同樣也使用cron表示式。與Quartz不同的是,Spring3.0不支援年,而Quartz支援年。但這點好象並不是非常重要。
cron表示式:-是用空格分開的時間欄位,不使用年。
*(秒0-59)
*(分鐘0-59)
*(小時0-23)
*(日期1-31)
*(月份1-12或是JAN-DEC)
*(星期1-7或是SUN-SAT)
例子:
"0 0 08 * * ?" 每天上午8點觸發
"0 15 10 ? * *" 每天上午10:15觸發
"0 15 10 * * ?" 每天上午10:15觸發
"0 15 10 * * ? *" 每天上午10:15觸發
"0 15 10 * * ? 2005" 2005年的每天上午10:15觸發
"0 * 14 * * ?" 在每天下午2點到下午2:59期間的每1分鐘觸發
"0 0/5 14 * * ?" 在每天下午2點到下午2:55期間的每5分鐘觸發
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發
"0 0-5 14 * * ?" 在每天下午2點到下午2:05期間的每1分鐘觸發
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發
"0 15 10 ? * MON-FRI" 週一至週五的上午10:15觸發
"0 15 10 15 * ?" 每月15日上午10:15觸發
"0 15 10 L * ?" 每月最後一日的上午10:15觸發
"0 15 10 ? * 6L" 每月的最後一個星期五上午10:15觸發
"0 15 10 ? * 6L 2009-2019" 2009年至2019年的每月的最後一個星期五上午10:15觸發
"0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發
*/5 * * * * 6-7 :: 每個周6到週日,每隔5秒鐘執行一次。
*/1 * * 7-9 1-2 1-7 :: 1月到2月中的7號到9號,且必須要滿足週一到週日,每隔1秒鐘執行一次。
*/1 * * 7-9 1,5 1-7 :: 注意裡面的,(逗號),只有1月和5月的7到9號,且必須要滿足週一到週日,每一秒鐘執行一次。
*/1 17-59 * 7-9 1,5 1-7 :: 只解釋17-59,是指從第17分鐘到第59分鐘,在指定的時間內,每一秒種執行一次
* 17-59 * 7-9 1,5 1-7 :: 此程式碼的功能與上面完全相同。如果不寫秒即為每一秒執行一次。
59 19-23 * 7-9 1,5 1-7 :: 19分-23分的每59秒鐘時只執行一次。
59 19,26 * 7-9 1,5 1-7 :: 注意裡面的,(逗號),是指只有19分或是26分的56秒鐘時執行一次。
* * 16-23 7-9 1,5 1-7 :: 定義每天的16點到23點每一秒鐘執行一次。
59 59 23 * * 1-5 :: 定義每週1到周5,晚上23:59:59秒只執行一次。
這個相當用有。可以工作時間每天給使用者發郵件。
fixedRate,fixedDelay,cron執行差異:
1.initialDelay :初次執行任務之前需要等待的時間:
@Scheduled(initialDelay =5000)
public void doSomething() {
System.out.println("initialDalay");
}
2.fixedDelay:每次執行任務之後間隔多久再次執行該任務:
@Scheduled(fixedDelay = 5000)
void doSomethingWithDelay(){
System.out.println("I'm doing with delay now!");
}
3.fixedRate:執行頻率,每隔多少時間就啟動任務,不管該任務是否啟動完成:
@Scheduled(fixedRate = 5000)
void doSomethingWithRate(){
System.out.println("I'm doing with rate now!");
}
4.cron=“”設定時分秒等具體的定時,網上很很多相關列子。(上一次任務未完成,不啟動) @Scheduled(cron = "0/5 * * * * *")
void doSomethingWith(){
System.out.println("I'm doing with cron now!");
}
在Spring3.0中引用了新的名稱空間-task:
task:scheduler 用於定義一個ThreadPoolTaskScheduler,並可以指定執行緒池的大小,即pool-size.所有任務佇列都將會在指定大小的執行緒池中執行:
定義如下:
<!-- 對於同一個Pojo可以宣告多次,並設定標記屬性 -->
<bean id="one" class="cn.itcast.schedule.One">
<property name="task" value="A"></property>
</bean>
<bean id="two" class="cn.itcast.schedule.One">
<property name="task" value="B"></property>
</bean>
<bean id="three" class="cn.itcast.schedule.One">
<property name="task" value="C"></property>
</bean>
<!-- 宣告一個具有兩個執行緒的池,每一個物件將獲取同樣的執行機會 -->
<task:scheduler id="sch" pool-size="2"/>
<!-- 引用執行緒池 -->
<task:scheduled-tasks scheduler="sch">
<!-- 引用Spring Bean並設定呼叫的方法的時間間隔 -->
<task:scheduled ref="one" method="doSomeThing" fixed-delay="#{1000*3}"/>
<task:scheduled ref="two" method="doSomeThing" fixed-delay="#{1000*3}"/>
<task:scheduled ref="three" method="doSomeThing" fixed-delay="#{1000*3}"/>
</task:scheduled-tasks>
<!-- 配置一個定時執行的任務 -->
<bean id="work" class="cn.itcast.schedule.Two"/>
<task:scheduler id="sendMail"/>
<task:scheduled-tasks scheduler="sendMail">
<!-- 定義在1月8號19:37:1秒執行一次,無論是周幾 -->
<task:scheduled ref="work" method="work" cron="1 37 19 8 1 *"/>
</task:scheduled-tasks>
定義好之後,正常啟動容器即可,只有條件符合,即會按要求執行任務。
相關推薦
spring基於task標籤和通過xml配置實現任務的區別
spring3.x可以通過<task>標籤輕易的定義定時任務,而且不需要依賴第三方jar包如quartz等,這個是spring自己的實現,但不支援叢集,其cron表示式也不支援年。 我們可以簡單的通過以下配置: 匯入maven座標:<dependency&g
37--Spring 基於tx標籤和基於@Transactional註解的宣告式事物介紹
上一節中已經對Spring事物的一些基本概念和核心介面做了簡介,並且演示了程式設計式事物實現,接下來介紹Spring中的另一種事物管理實現–宣告式事物。 其底層建立在 AOP 的基礎之上,對方法前後進行攔截,然後在目標方法開始之前建立或者加入一個事務,在執行完目標方法之後根據執行情況
Spring-通過xml配置實現AOP
1.定義切面類 如下函式,將beforeMethod應用到其他函式中。 package test; import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void beforeM
Spring通過XML配置c3p0連線池和dao層註解注入使用 jdbcTemplate
Spring通過註解配置c3p0連線池和dao使用 jdbcTemplate 1.Spring配置c3p0連線池 第一步:匯入c3p0的jar包 第二步:建立Spring配置檔案,配置連線池 平常我們寫c3p0連線池時是這樣寫的:
Spring application.xml配置和web.xml配置
事務 mybatis 配置 ive word tran ransac pro web application.xml<!--掃描包—-> <context:compent-scan package="com.bdqn.exam">&
Spring 通過XML配置裝配Bean
使用XML裝配Bean需要定義對於的XML,需要引入對應的XML模式(XSD)檔案,這些檔案會定義配置Spring Bean的一些元素,簡單的配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.s
web.xml 中spring-servlet.xml 和 application.xml 配置位置及含義
在我們進行 Spring-servlet 進行開發的時候,經常會遇到配置檔案配置的問題,要徹底的解決這個問題,我們需要了解 springMVC 設計的基本架構 1.SpringMVC 的配置分為兩部分 application.xml 和 spring-servl
【Mybatis】mapper動態代理和sqlMapconfig.xml配置標籤
1.mapper動態代理 使用mapper對映來,聯絡sql語句和程式碼函式。 步驟一:在上一篇基礎的上,新增一個介面,要求與UserMapper的xml配置檔案同名,加入在xml中的所有sql的id相同的方法,即方法名相同,入參和返回值都相同 步驟二:修改usermapper的名稱
spring專案中通過xml配置快取(cache)記錄貼
配置一個spring預設實現的快取(cache)<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:
spring配置mybatis自動掃描*mapper.java和*mapper.xml配置檔案
預設mybatis需要在配置檔案中載入每個mapper.xml,例如: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD
spring-servlet.xml 和 application.xml 配置含義及bean被載入兩次的問題
最近在遷移一個專案,原本是RPC服務與API水平拆分的,現在需要垂直拆分,每個RPC服務自己提供http介面,我負責遷移RPC部分,另一個同事負責遷移API部分,涉及到一些配置遷移的時候出現了些小狀
Quartz-Spring整合Quartz通過XML配置的方式
概述 Spring為建立Quartzde Scheduler、Trigger和JobDetail提供了方便的FactoryBean類,以便能夠在Spring容器中享受注入的好處。 此外,Spring還通了一些便利的工具類,用於直接將Spring中的
Spring 通過XML配置檔案以及通過註解形式來AOP 來實現前置,環繞,異常通知,返回後通知,後通知
本節主要內容: 一、Spring 通過XML配置檔案形式來AOP 來實現前置,環繞,異常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP 環繞通知 XML配置使用案例 3. Spring AOP
Spring mvc通過xml配置檔案方式實現簡單HelloWorld
實現Spring MVC有兩種不同的方式:基於XML配置檔案和基於註解。 這裡,我們使用XML檔案的方式來實現。 首先,我們需要在Eclipse或者是MyEclipse中新建一個web專案,並將Spr
Spring-通過xml配置檔案實現切面(AOP)
使用註解的方式實現AOP: 1、業務類 PersonService.java/* *@Author swxctx *@time 2016年9月22日 */ package com.sw.serv
SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋
con initial -m and 整理 .get 尺寸 internal 頁面 SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋 2016-04-14 23:40 13030人閱讀 評論(2) 收藏 舉報
Spring AMQP 源碼分析 08 - XML 配置
logs blog http cti ase xsd -name border 分析 ### 準備 ## 目標 通過 XML 配置文件使用 Spring AMQP ## 前置知識 《Spring AMQP 源碼分析 07 - MessageL
Spring框架—— IOC容器和Bean的配置
單引號 framework 將不 配置信息 init 字符串連接 生命 release exp 1 IOC和DI ①IOC(Inversion of Control):反轉控制。 在應用程序中的組件需要獲取資源時,傳統的方式是組件主動的從容器中獲取所需要的資源,在這樣的模
spring入門(二) 使用註解代替xml配置
sca utf bean getname app code 其他 入門 lns 1.導包(略) 2.applicationContext.xml如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <b
微服務spring cloud—Hystrix簡介和通過方式整合H
Hystrix簡介 Hystrix是由Netflix開源的延遲和容錯庫,用於隔離訪問遠端系統、服務或者第三方庫,防止級聯失敗,從而提升系統的可用性與容錯性。Hystrix主要通過以下幾點實現延遲和容錯。 包裹請求:使用HystrixCommand(或HystrixObservable