導航:首頁 > 文件管理 > spring定時配置文件

spring定時配置文件

發布時間:2023-05-29 13:23:34

1. 如何配置Spring定時器准確運行時間

Spring中有自帶任務調度框架Quartz ,直接在xml配置文件中配置就好。

2. spring定時器如何配置

上面這種仁兄所言極是, spring定時器目前主要就是quartz來實現, 如果自己手寫代碼不用spring管理的話, TimeTask應該是不錯的選擇, 不過我平時測試時常有發現TimeTask執行任務時, 有時一個任務重復執行多次, 所以建議還是採用Quartz的方式會好一些。。而且它操作起來也會更加的靈活。。。

3. spring定時器配置在哪個xml

spring定時器目前有兩種方式、一種是走配置文件,一種是註解下面來介紹這兩種方式。

一、配置文件實現

<?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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.1.xsd

">

<!-- 詳細信息 -->

<bean id="methodInvokingJobDetail"class="org.springframework.scheling.quartz.">

<property name="targetObject"><ref bean="mainServiceImplTwo"/></property>

<property name="targetMethod"><value>springTrigger</value></property>

<!-- 一個定時器,可能會重復的執行,有可能第一次還沒有執行完,第二次就開始了

設置此參數保證第一次定時器,還沒有執行完時,不會執行第二次 -->

<property name="concurrent"><value>false</value></property>

</bean>

<!-- 觸發器 3.2的實現方式 CronTriggerBean

<bean id="cronTrigger" class="org.springframework.scheling.quartz.CronTriggerBean">

<property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>

<property name="cronExpression">

<value>0 * * * * ?</value>

</property>

</bean>

-->

<!-- 觸發器 4.1及以後的實現方式 CronTriggerFactoryBean -->

<bean id="cronTrigger" class="org.springframework.scheling.quartz.CronTriggerFactoryBean">

<property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>

<property name="cronExpression">

<value>0 * * * * ?</value>

</property>

</bean>

<!-- 定時任務列表 -->

<bean class="org.springframework.scheling.quartz.SchelerFactoryBean">

<property name="triggers">

<list>

<ref bean="cronTrigger"/>

</list>

</property>

</bean>

</beans>

<value>0 * * * * ?</value> 表示時間配置:時間大小由小到大排列,從秒開始,順序為 秒,分,時,天,月,年 *為任意 ?為無限制。

marven 引入定時器需要的包(不包含其他spring的包。)

<dependency> <!-- spring 定時器需要 -->

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.1.4.RELEASE</version>

</dependency>

<dependency> <!-- spring 定時器需要 -->

<groupId>org.quartz-scheler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.1</version>

</dependency>

<dependency> <!-- spring 定時器需要 -->

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.6.6</version>

</dependency>

具體時間設定可參考

"0/10 * * * * ?" 每10秒觸發

"0 0 12 * * ?" 每天中午12點觸發
"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 2002-2005" 2002年至2005年的每月的最後一個星期五上午10:15觸發
"0 15 10 ? * 6#3" 每月的第三個星期五上午10:15觸發
每隔5秒執行一次:*/5 * * * * ?
每隔1分鍾執行一次:0 */1 * * * ?
每天23點執行一次:0 0 23 * * ?
每天凌晨1點執行一次:0 0 1 * * ?
每月1號凌晨1點執行一次:0 0 1 1 * ?
每月最後一天23點執行一次:0 0 23 L * ?
每周星期天凌晨1點實行一次:0 0 1 ? * L
在26分、29分、33分執行一次:0 26,29,33 * * * ?
每天的0點、13點、18點、21點都執行一次:0 0 0,13,18,21 * * ?

二、註解形式實現

在springmvc.xml裡面(根據自己的實際代碼改變)

xmlns 多加下面的內容、
xmlns:task="http://www.springframework.org/schema/task"

然後xsi:schemaLocation多加下面的內容、

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd

<!-- task:註解定時器使用 這種配置有錯誤 會報找不到 org.springframework.scheling.TaskScheler 這個bean(這其實是一個介面)
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation."/>

-->

<!-- 這種配置是正常的 --->

<task:annotation-driven scheler="qbScheler" mode="proxy"/>
<task:scheler id="qbScheler" pool-size="10"/>

要放在

<mvc:annotation-driven/>
<context:component-scan base-package="com">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

之後

代碼實現類實現如下:

@Component
public class UsersTimerImpl implements IUsersTimer{
//更新 支付密碼狀態
/// 定時器註解調用
@Scheled(cron = "00 00 00 * * *")
public void updatePayStatus(){
//執行邏輯的代碼
}

}

4. spring定時任務配置問題,求教

(1)在Spring的配置文件中添加定時任務相關配置:

xml配置的頭文件中添加:

xmlns:task="http://www.springframework.org/schema/task"

以及在xsi:schemaLocation中添加:

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd

最後添加:

	<context:component-scanbase-package="cn.zifangsky.task"/>
<task:executorid="executor"pool-size="5"/>
<task:schelerid="scheler"pool-size="10"/>
<task:annotation-drivenexecutor="executor"scheler="scheler"/>

其中,這里首先定義了Spring自動掃描定時任務所在的package,也就是「cn.zifangsky.task」。接著定義了兩個線程池以及啟用定時任務的掃描機制

(2)添加測試任務:

java">packagecn.zifangsky.task;

importjava.text.Format;
importjava.text.SimpleDateFormat;
importjava.util.Date;

importorg.springframework.scheling.annotation.Scheled;
importorg.springframework.stereotype.Component;

@Component
publicclassSimpleSpringTask{

/**
*每次任務執行完之後的2s後繼續執行
*/
@Scheled(fixedDelay=2000)
publicvoidsay(){
Datecurrent=newDate();
Formatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");

System.out.println("--------"+format.format(current)+"---------");
}

/**
*0分的時候列印
*/
@Scheled(cron="0****?")
publicvoidprint(){

System.out.println("當前是整分!!!");
}

}

上面第一個任務定義了每個任務執行完之後的2s之後再次執行,如果需要強制指定每隔多少時間執行一次任務,可以將上面的fixedDelay改成fixedRate,如:

	/**
*每隔兩秒執行一次本方法
*/
@Scheled(fixedRate=2000)
publicvoidsay(){
Datecurrent=newDate();
Formatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");

System.out.println("--------"+format.format(current)+"---------");
}

當然,上面的第二種任務形式類似於Linux下的crontab定時任務,幾個參數位分別表示:分鍾、小時、天(每月中的天)、月份以及星期。最後的那個問號毫無疑問就表示使用@Scheled註解標注的本個方法了

註:如果想要了解更多的關於Linux中使用crontab命令的用法可以參考我的這篇文章:https://www.zifangsky.cn/591.html

(3)測試:

運行這個項目後,最後控制台中的輸出如下:

註:上面的水印是我的個人博客。由於題主的問題不是一兩句文字可以描述清楚地,因此引用了我博客中的內容,請審核人員手下留情

PS:如果覺得對你有所幫助的話,望採納!!!

5. 如何在spring中配置定時任務

spring的定時任務配置分為三個步驟:
1、定義任務
2、任務執行策略配置
3、啟動任務
(程序中一般我們都是到過寫的,直觀些)

1、定義任務
<!--要定時執行的方法-->
<bean id="testTaskJob"
class="org.springframework.scheling.quartz.">
<property name="targetObject">
<!--指定要定時執行的方法所在類,將定時任務定義成bean-->
<ref bean="testTask" />
</property>
<property name="targetMethod">
<!--指定定時執行的方法-->
<value>execute</value>
</property>
<property name="concurrent">
<!--指定目標封裝為有狀態的任務,有狀態的任務不能並發執行,無狀態的任務可並發執行-->
<value>false</value>
</property>
</bean>

2、任務執行策略配置
(1)指定重復間隔的定時任務
<!-- 調度時間設置-->
<bean id="testTaskJobTrigger"
class="org.springframework.scheling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="testTaskJob" />
</property>
<!-- 延時啟動時間,單位ms -->
<property name="startDelay" value="60000"></property>
<!-- 重復間隔時間,單位ms -->
<property name="repeatInterval" value="60000">
</property>
</bean>

(2)按周期執行的任務

閱讀全文

與spring定時配置文件相關的資料

熱點內容
文件有哪些要求 瀏覽:484
cad打開時會出現兩個文件 瀏覽:65
什麼是轉基因網站 瀏覽:48
手柄設備有問題代碼43 瀏覽:921
怎麼他么怎麼又網路了 瀏覽:649
java會出現內存泄露么 瀏覽:617
蘋果4s鎖屏後怎麼還顯示歌曲 瀏覽:207
鴻蒙系統文件管理哪些可以刪除 瀏覽:550
ubuntuqt創建工程沒有配置文件 瀏覽:126
網站登錄變成其他網站怎麼處理 瀏覽:202
ug數控編程學校有哪些 瀏覽:203
java圖片上傳顯示 瀏覽:402
ppt的文件名後綴 瀏覽:902
ug編程軟體下載到哪個盤 瀏覽:359
炫酷字體APP下載的文件在哪裡 瀏覽:668
廊坊哪裡有少兒編程機構 瀏覽:312
cad新文件能找回來嗎 瀏覽:951
導出手機qq文件到u盤 瀏覽:456
電腦如何打開ppt文件怎麼打開方式 瀏覽:782
魅族鎖定區文件夾 瀏覽:357

友情鏈接