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

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

一、通过配置文件

1、任务运行类

  1. import org.springframework.stereotype.Service;
  2. @Service
  3. public class TaskTest{
  4.  
  5. public void Test(){
  6. System.out.println(new Date() + "定时任务開始…");
  7. }
  8. }

2、spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?
  2.  
  3. >
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. <strong>xmlns:task="http://www.springframework.org/schema/task"</strong>
  8. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  9. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  12. <strong>http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd</strong>
  13. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
  14. xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx">
  15.  
  16.   .
  17.   .
  18.   .
  19.   <context:component-scan base-package=" com.hp.task" />
  20.  
  21.   <task:scheduled-tasks>
  22. <task:scheduled ref="taskTest" method="test" cron="0/5 * * * * ?"/>
  23.   </task:scheduled-tasks>
  24.  
  25. </beans>

參数说明:ref參数是运行任务的类。method是类中须要运行的方法。cron运行表达式表示运行的时间及策略。

二、通过注解

1、任务运行类

  1. import org.springframework.scheduling.annotation.Scheduled;
  2. import org.springframework.stereotype.Component;
  3.  
  4. /**
  5. * 定时处理类
  6. *
  7. * @author zjj
  8. * @date 2015-6-30
  9. */
  10. @Component
  11. public class TaskTest{
  12. /**
  13. * 定时处理方法測试
  14. * 每5秒一次
  15. */
  16. @Scheduled(cron = "0/5 * * * * ?
  17.  
  18. ")
  19. public void Test() {
  20. System.out.println(new Date() + "定时任务開始…");
  21. }
  22. }

这里须要两个注解:

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

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

2、Spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:task="http://www.springframework.org/schema/task"
  6. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  10. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
  12. xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx">
  13.  
  14.   <context:component-scan base-package=" com.hp.task" />
  15.  
  16.   <!—开启这个配置,spring才干识别@Scheduled注解 -->
  17.   <task:scheduler id="scheduler" pool-size="10" />
  18.   <task:executor id="executor" pool-size="10" />
  19.   <task:annotation-driven scheduler="scheduler" executor="executor" />
  20.  
  21. </beans>

总结

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

Spring Task 定时任务的更多相关文章

  1. Spring task定时任务执行一段时间后莫名其妙停止的问题

    前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...

  2. Spring Task定时任务的配置和使用详解

    spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.or ...

  3. Quartz和Spring Task定时任务的简单应用和比较

    看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...

  4. Spring task定时任务

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  5. Spring Task定时任务Scheduled

    Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...

  6. spring boot 之 spring task(定时任务)

    cron:通过表达式来配置任务执行时间cron表达式详解 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素.按顺序依次为: 秒(0~59)分钟(0~59)3 小时(0~23)4  天(0 ...

  7. Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)

    原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...

  8. Spring Task中的定时任务无法注入service的解决办法

    1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...

  9. Spring 使用介绍(十二)—— Spring Task

    一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...

随机推荐

  1. Python爬虫-代理池-爬取代理入库并测试代理可用性

    目的:建立自己的代理池.可以添加新的代理网站爬虫,可以测试代理对某一网址的适用性,可以提供获取代理的 API. 整个流程:爬取代理 ----> 将代理存入数据库并设置分数 ----> 从数 ...

  2. 数据结构实验1:C++实现静态顺序表类

    写了3个多小时,还是太慢了.太菜了! 图1 程序运行演示截图1 实验1 1.1 实验目的 熟练掌握线性表的顺序存储结构. 熟练掌握顺序表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的顺序 ...

  3. tarjan 学习记

    1.强连通分量是什么 强连通分量指的是部分点的集合如果能够互相到达(例如 1→3,3→2,2→1(有向图)这种,132每个点都能互相抵达) 或者说,有一个环,环上点的集合就是一个强连通分量 2.那怎么 ...

  4. 【Codeforces 1107D】Compression

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 先把所给的压缩形式的字符串转成二进制 然后对获得的01数组做一个前缀和(a[i][j]=以(i,j)为右下角,(1,1)为左上角的矩形内的数字 ...

  5. There is no getter for property named 'id' in class 'java.lang.String'

    https://blog.csdn.net/u011897392/article/details/46738747 使用mybatis传入参数,如果在mappin.xml中使用<if>标签 ...

  6. ORACLE-023:令人烦恼的 ora-01722 无效数字

    https://blog.csdn.net/yysyangyangyangshan/article/details/51762746

  7. ul标签中,li标签的移除、属性值获取

  8. POJ 1273 Drainage Ditches【图论,网络流】

    就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图 ...

  9. ubuntu 12.04 64bit 安装 teamviewer 8.0

    1. 在http://www.teamviewer.com下载teamviewer_linux_x64.deb 2.sudo dpkg -i teamviewer_linux_x64.deb 3.如果 ...

  10. 蓝桥杯 算法训练 最短路 [ 最短路 bellman ]

    传送门   算法训练 最短路   时间限制:1.0s   内存限制:256.0MB     锦囊1   锦囊2   锦囊3   问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证 ...