Java利用Spring作定时器

Spring中有专门的配置作定时器---applicationContext-task.xml

第一步:

在Web.xml文件中加载applicationContext-task.xml.

<span style="white-space:pre">	</span><context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/applicationContext-task.xml,
			/WEB-INF/beans.xml,
		</param-value>
	</context-param>
		<context-param>
		<param-name>spring.profiles.default</param-name>
		<param-value>development</param-value>
	</context-param></span>

第二步:

在applicationContext-task.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:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
				http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
				http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
default-lazy-init="true">

    <!-- 每隔5分钟 拒绝特殊标记合同-->
<span style="white-space:pre">	</span><task:scheduled-tasks> 
	 <task:scheduled ref="loanServiceImpl" method="autoRefuseLoan" fixed-rate="300000"/>
	 <!-- 每天早上9点执行短信通知 -->  
   <task:scheduled ref="smsNotifyServiceImpl" method="smsNotify" cron="0 05 09 * * ?"/>
    <!-- 每天晚上10点执行代扣 -->  
     <task:scheduled ref="autopayService" method="autopay" cron="0 30 22 * * ?"/>  
  <!-- 每天晚上22:00自动将老系统中前天22:00-当天22:00的还款,迁移到新系统中 -->
     <task:scheduled ref="loanServiceImpl" method="repayDataTrans" cron="0 00 12 * * ?"/>
	</task:scheduled-tasks> </beans>


cron参数说明(http://blog.sina.com.cn/s/blog_54088efd0100j26m.html)

秒(0~59)

分钟(0~59)

小时(0~23)

天(月)(0~31,但是你需要考虑你月的天数)

月(0~11)

天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

7.年份(1970-2099)




猜你喜欢

转载自blog.csdn.net/qq844579582/article/details/52447604
今日推荐