在項目中使用定時任務是常有的事,好比天天定時進行數據同步或者備份等等。java
之前在從事C語言開發的時候,定時任務都是經過寫個shell腳本,而後添加到linux定時任務中進行調度的。linux
如今使用SpringMVC以後,一塊兒都變得簡單了o(∩_∩)o spring
有兩種配置方式,我都分別講講,可是看了後你確定只會選擇後面那種,沒錯! 我也是用後面那種方式shell
第一種配置方式:這個比較複雜,配置的地方有點多,稍不留意就不成功,具體看代碼了spring-mvc
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 每隔一個小時從新獲取一次token --> <!-- 1.要調用的工做類 --> <bean id="refreshAccessTokenJob" class="com.xiao.weixin.quartz.RefreshAccessToken"/> <!-- 2.定義調用對象和調用對象的方法 --> <bean id="refreshAccessTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 調用的類 --> <property name="targetObject"> <ref bean="refreshAccessTokenJob"/> </property> <!-- 調用類中的方法 --> <property name="targetMethod"> <value>work</value> </property> </bean> <!-- 3.定義觸發時間 --> <bean id="refreshAccessToken" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="refreshAccessTokenTask"/> </property> <!-- cron表達式 --> <property name="cronExpression"> <value>0 0 */2 * * ?</value> </property> </bean> <!-- 4.總管理類 若是將lazy-init='false'那麼容器啓動就會執行調度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="refreshAccessToken"/> </list> </property> </bean> </beans>
第二種配置方式:強烈推薦的這種mvc
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="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.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <task:scheduled-tasks> <task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" /> </task:scheduled-tasks> <bean id="quartzTestBean" class="xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/> </beans>
這裏面很簡單,直接調用service接口實現類中的方法就能夠了,maven
maven配置中加上spa
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
好比個人實現類是這樣的:.net
package xiaochangwei.zicp.net.service; import java.util.Date; import org.springframework.stereotype.Service; import xiaochangwei.zicp.net.common.tools.DateUtil; @Service public class QuartzTestServiceImpl implements QuartzTestService { public void quartzJobTestMethod() { System.out.println("定時任務執行:" + DateUtil.getFormatDate(new Date())); } }
每隔五秒打印一個當前時間3d
執行結果以下:
定外配置任務多久執行也很簡單:
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
上面這個表示五秒鐘執行一次
總共有五個*,從前日後表示秒,分,時。。。。。。。
多少秒執行一次說了,說多少分執行一次
cron="* */2 * * * ?" 對麼? 這樣不對哈,要將前面的星改成0 cron="0 */2 * * * ?" 表示兩分執行一次同理 若是兩小時執行一次,則前面兩個都是0 哦 具體規則請百度cron吧 so easy !