springmvc——定时任务

今天,由于项目中需要实现一个定时监控自动给用户发邮件进行提醒的功能,为了实现这一功能,我想到使用定时任务,知道springmvc中就有自带的定时任务,下面就整理一下,方便以后使用:

主要有两种方式实现,基于XML的和基于注解的,当然现在越来越多的偏爱于基于注解的实现方式

(一)基于XML的

1、配置文件

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<task:annotation-driven /> <!-- 定时器开关-->

	<bean id="myTaskXml" class="com.lt.task.MyTaskXml"></bean>

	<task:scheduled-tasks>
		<!-- 
			这里表示的是每隔五秒执行一次 
		-->
		<task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
		<task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
	</task:scheduled-tasks>
	
    <!-- 自动扫描的包名 -->  
    <context:component-scan base-package="com.lt.task" />
    
</beans>

2、定时任务

 

package com.lt.task;


/**
 * 基于xml的定时器
 * @author lt
 */
public class MyTaskXml {
	
	
	public void show(){
		System.out.println("XMl配置文件方式show()");
	}
	
	public void print(){
		System.out.println("XMl配置文件方式print()");
	}
}

(二)基于注解的

 

1、配置文件

 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<task:annotation-driven /> <!-- 定时器开关-->
	
    <!-- 自动扫描的包名 -->  
    <context:component-scan base-package="com.lt.task" />
    
</beans>

2、定时任务

 

package com.lt.task;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 基于注解的定时器
 * @author lt
 */
@Component
public class MyTaskAnnotation {
	
	/** 
     * 定时计算。每天凌晨 01:00 执行一次 
     */  
    @Scheduled(cron = "0 0 1 * * *") 
	public void show(){
		System.out.println("Annotation:注解run()");
	}
	
    /** 
     * 心跳更新。启动时执行一次,之后每隔2秒执行一次 
     */  
    @Scheduled(fixedRate = 1000*2) 
	public void print(){
		System.out.println("Annotation:注解print()");
	}
}

 

特殊字符说明:

“,”字符:列出枚举值。例如:0 26,29,33 * * * ?    表示在26分、29分、33分执行一次。

“-”字符:指定一个值的范围。例如:0 0-5 14 * * ?    表示每天从下午2点开始到2:05分结束,每1分钟执行一次

“*”字符:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。 例如:0 * 14 * * ?    表示每天从下午2点开始到2:59分结束,每1分钟执行一次

“/”字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m。例如:*/5 * * * * ?    表示每隔5秒执行一次

“L”字符:用在日表示一个月中的最后一天,用在周表示该周最后一个星期,也就是周日。例如:0 0 23 L * ?    表示每月最后一天23点执行一次。

“W”字符:指定离给定日期最近的工作日(周一到周五)。例如:在字段月域里使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份。

“LW”字符:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。

“#”字符:表示该月第几个周X。例如:6#3表示该月第3个周五

“?”字符:表示不确定的值。只能用在“月”和“周”两个域。它也匹配域的任意值,但实际不会。因为“月”和“周”会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 0 0 0 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。

“C”字符:可用于“日”和“周几”字段,它是"calendar"的缩写。它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日历,那它等同于包含全部日历。“日”字段值为"5C"表示"日历中的第一天或者5号以后",“周几”字段值为"1C"则表示"日历中的第一天或者周日以后"。

 

参考文章:

 

http://www.cnblogs.com/liaojie970/p/5913272.html

 

让我们一起遨游在代码的海洋里!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值