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() + "定时任务開始…");
}
}
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 定时任务的更多相关文章
- Spring task定时任务执行一段时间后莫名其妙停止的问题
前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...
- Spring Task定时任务的配置和使用详解
spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.or ...
- Quartz和Spring Task定时任务的简单应用和比较
看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...
- Spring task定时任务
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Spring Task定时任务Scheduled
Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...
- spring boot 之 spring task(定时任务)
cron:通过表达式来配置任务执行时间cron表达式详解 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素.按顺序依次为: 秒(0~59)分钟(0~59)3 小时(0~23)4 天(0 ...
- Quartz cron 表达式(linux 定时器,java 定时任务,spring task定时任务)
原文地址:https://blog.csdn.net/feng27156/article/details/39293403 Quartz cron 表达式的格式十分类似于 UNIX cron 格式,但 ...
- Spring Task中的定时任务无法注入service的解决办法
1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
随机推荐
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- pandas.read_csv 报ssl.SSLError
- 转载 vue的基础使用
转载https://www.cnblogs.com/majj/p/9957597.html#top vue的介绍 前端框架和库的区别 nodejs的简单使用 vue的起步 指令系统 组件的使用 过滤器 ...
- 数据结构实验2:C++实现单链表类
太简单了,直接贴题目然后上代码. 题目: 实验2 2.1 实验目的 熟练掌握线性表的链式存储结构. 熟练掌握单链表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的链式存储结构,并设计相关算 ...
- Linux 命令大全 - 管理文件和目录的命令
1.pwd 显示当前目录 该命令的英文解释为print working directory(打印工作目录).输入pwd命令,Linux会输出当前目录. 2.cd 命令用来改变所在目录 cd / 转到根 ...
- 大数据学习——hadoop安装
上传centOS6.7-hadoop-2.6.4.tar.gz 解压 tar -zxvf centOS6.7-hadoop-2.6.4.tar.gz hadoop相关修改配置 1 修改 /root/a ...
- 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【Zeller公式计算星期几】HDU 6112 今夕何夕
acm.hdu.edu.cn/showproblem.php?pid=6112 [思路] 公式计算即可,注意特判2月29号 Zeller公式里,计算出的week不能直接模7,要保证week是正数 [A ...
- BZOJ1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
n<=200个点m<=40000条边无向图,求 t次走不经过同条边的路径从1到n的经过的边的最大值 的最小值. 最大值最小--二分,t次不重边路径--边权1的最大流. #inclu ...
- 内存管理——(exceptional C++ 条款9,条款10)
C++的各个内存区域: (1)常量数据(const data)区 常量数据区存储的是字符串等在编译期间就能确定的值,在整个程序的生命周期内,这里的数据都是可用.区域内所有的数据都是 只读的. (2)栈 ...