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 ...
随机推荐
- [LOJ] #2363「NOIP2016」愤怒的小鸟
精度卡了一个点,别人自带大常数,我自带大浮点误差qwq. 听了好几遍,一直没动手写一写. f[S]表示S集合中的猪被打死的最少抛物线数,转移时考虑枚举两个点,最低位的0为第一个点,枚举第二个点,构造一 ...
- 彻底卸载WIN10 OneDrive
彻底卸载WIN10 OneDrive @ECHO OFF %SystemRoot%\SysWOW64\OneDriveSetup.exe /uninstall RD "%UserProfil ...
- <Spring Data JPA>二 Spring Data Jpa
1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- HTML5新增的主体元素article、section、nav、aside、time元素和pubdate属性
article artticle元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或者报刊中的文章,一篇论坛帖子,一段用户评论或者独立的插件或其他任何独立的内容. ...
- MVC常见框架
Struts Struts是Apache软件基金下Jakarta项目的一部分.Struts框架的主要架构设计和开发者是Craig R.McClanahan.Struts 是Java Web MVC框架 ...
- CF 977 F. Consecutive Subsequence
题意: 第一场div3, 求的是一个序列中最长连续(a,a+1,a+2...)子序列. 分析: 设一个DP[i] 表示 序列以i结尾的最长长度, 一开始都设为0. 那么如果这个数是a, 他的最长长度就 ...
- JS替换回车换行符
replace(/\r/ig, '<br>').replace(/\n/ig, '<br>')
- 谢孟军:The State of Go | ECUG Con 精粹系列
本月 17 日,Go 1.8 版本火热发布.相较于以往的版本,Go 1.8 具体有哪些新的特性呢?想必这是不少 Gopher 们热切关注和讨论的问题.作为著名的Golang 布道者,Gopher Ch ...
- codeforces #301 div2
A:简单题 每次判断向上转快,还是向下转快即可 #include <cstdio> #include <cstring> #include <iostream> # ...
- 【二分图匹配】E. 过山车
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/E [题意] 裸的最大匹配 [教训] 一开始边数开了k,建的是无向图,结果T了,改 ...