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

  在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>

总结

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

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. Linux中一些约定俗成的文件扩展名

    注:Linux中的所有内容均以文件的形式保存,但不依靠扩展名区分文件类型(根据权限区分),约定俗成的文件扩展名是为了方便管理员对文件进行区分 压缩包:“*.gz”.“*.bz2”.“*.tar.bz2 ...

  2. Android 图片设置圆角 方法之二

    Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片.接下来我们再介绍一种方法. 首先, 自定义ImageView: android:id="@+id/iv" ...

  3. Python中 模块、包、库

    模块:就是.py文件,里面定义了一些函数和变量,需要的时候就可以导入这些模块. 包:在模块之上的概念,为了方便管理而将文件进行打包.包目录下第一个文件便是 __init__.py,然后是一些模块文件和 ...

  4. 2019年,Python工程师必考的6个面试题,Python面试题No5

    第1题:Python里面如何实现tuple和list的转换? 函数tuple(seq)可以把所有可迭代的(iterable)序列转换成一个tuple, 元素不变,排序也不变 list转为tuple: ...

  5. 【Codeforces 1051D】Bicolorings

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] dp[i][j][k]表示前i列,有j个联通块下,最后一列的状态为k的方案数 k如果为1的话,表示最后一列两个块不一样,k如果为0表示一样 枚 ...

  6. Mybatis 缓存策略

    听极客学院笔记 使用mybatis的缓存需要以下三步 一.在mybatis的config.xml中开启缓存 <settings> <setting name="cacheE ...

  7. 【java基础 4】树形结构数据呈现的非递归算法(循环)实现

    一.基本概况 上一篇博客介绍到用递归实现树结构数据的查找,那么这篇博客,我就结合自己对于树的理解,然后用一种非递归的方式进行树结构数据的处理.首先,改造数据库表设计,加入度的概念: 首先,layer的 ...

  8. [luoguP2216] [HAOI2007]理想的正方形(二维单调队列)

    传送门 1.先弄个单调队列求出每一行的区间为n的最大值最小值. 2.然后再搞个单调队列求1所求出的结果的区间为n的最大值最小值 3.最后扫一遍就行 懒得画图,自己体会吧. ——代码 #include ...

  9. msp430项目编程04

    msp430中项目---TFT彩屏显示 1.TFT彩屏工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习

  10. HDU 3609 二分图多重匹配

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...