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

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

一、通过配置文件

1、任务运行类

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

2、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: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指该方法运行的时间及策略

2、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: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>

总结

  两种方式实现定时任务都是非常方便的,可是都存在同一个问题。定时策略在项目启动后我们无法动态改动。要改动就须要重新启动服务,如何做到定时策略动态设定也将是兴许解决的问题。

最新文章

  1. 管理node的版本
  2. 关于git中git pull --rebase中的一些坑
  3. Win7 64位 VS2015及MinGW环境编译矢量库agg-2.5和cairo-1.14.6
  4. 用AutoCompleteTextView实现历史记录提示
  5. Ibatis的环境搭建以及遇到的问题解决
  6. 【wikioi】1010 过河卒
  7. zoj 3557 How Many Sets II
  8. linux 命令小结
  9. CCF 认证
  10. 在java中高效的计数器
  11. Feign源码解析
  12. 网络流Dinic(本篇介绍最大流)
  13. Diango 框架起步
  14. SQLOS任务调度算法
  15. SIP消息
  16. 半夜两点灵光一现想出来的一个demo
  17. Usefull Resources
  18. shell之arp命令
  19. 修改后无警告全面支持non-ARC以及ARC的OpenUDID
  20. ACM 第二十天

热门文章

  1. INFORMATION_SCHEMA 表
  2. CSS3---媒体查询与响应式布局
  3. K8S部署
  4. C语言学习9
  5. solr中的schema.xml(managed-schema)文件解读
  6. 【URAL 1486】Equal Squares(二维哈希+二分)
  7. luogu2633 Count on a tree
  8. xtu字符串 D. 病毒侵袭
  9. PC硬件以及引导加载器
  10. 【bzoj1055】[HAOI2008]玩具取名