Spring Task 定时任务

  所谓定时任务,就是根据我们设定的时间定时执行任务,就像定时发邮件一样,设定时间到了,邮件就会自动发送。

  在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task。同Spring的其他功能一样,我们既可以通过配置文件也可以通过注解形式来实现。


一、通过配置文件

1、任务执行类

import org.springframework.stereotype.Service;  
@Service  
public class TaskTest{  
      
    public void Test(){  
        System.out.println(new Date() + "定时任务开始…");  
    }  
}

2spring配置文件

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	<strong>xmlns:task="http://www.springframework.org/schema/task"</strong>
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		<strong>http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd</strong>
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx">

  .
  .
  .
  <context:component-scan base-package=" com.hp.task" /> 

   <task:scheduled-tasks>   
        <task:scheduled ref="taskTest" method="test" cron="0/5 * *  * * ?"/>   
  </task:scheduled-tasks>  
  
</beans>

参数说明:ref参数是执行任务类,method是类中需要执行的方法,cron执行表达式表示执行的时间及策略。


二、通过注解

1、任务执行类

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

/**
 * 定时处理类
 * 
 * @author zjj
 * @date 2015-6-30
 */
@Component
public class TaskTest{
	/**
	 * 定时处理方法测试
	 * 每5秒一次
	 */
	@Scheduled(cron = "0/5 * *  * * ?")
	public void Test() {
		System.out.println(new Date() + "定时任务开始…");  
	}
}

这里需要两个注解:

  @Component:将该类完成bean创建和自动依赖注入

  @Scheduled:将该方法定义为Spring定时调用的方法,其中cron指该方法执行的时间及策略


2Spring配置文件

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx">

  <context:component-scan base-package=" com.hp.task" /> 

  <!—开启这个配置,spring才能识别@Scheduled注解   -->  
  <task:scheduler id="scheduler" pool-size="10" />
  <task:executor id="executor" pool-size="10" />
  <task:annotation-driven scheduler="scheduler" executor="executor" />

</beans>

总结

  两种方式实现定时任务都是很方便的,但是都存在同一个问题,定时策略在项目启动后我们无法动态修改,要修改就需要重启服务,怎样做到定时策略动态设定也将是后续解决的问题。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值