Spring 3.0以后版本的定时任务
自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd">
- <!--
- 这里加入了
- xmlns:task="http://www.springframework.org/schema/task"
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task-3.0.xsd
- -->
- <task:annotation-driven /> <!-- 定时器开关-->
- <bean id="taskTest" class="com.jungle.test.TaskTest"></bean>
- <task:scheduled-tasks>
- <!--
- 这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~)
- -->
- <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />
- <task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>
- </task:scheduled-tasks>
- import java.util.Date;
- public class TaskTest {
- public void say() {
- System.out.println("这个真好用!!!" + new Date());
- }
- public void hello(){
- System.out.println("hello!!!");
- }
- }
第二种:spring注解形式
也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:
- @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface Scheduled
- {
- public abstract String cron();
- public abstract long fixedDelay();
- public abstract long fixedRate();
- }
measured from the start time of the previous task. The time unit value
is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- @Component(“taskJob”)
- public class TaskJob {
- @Scheduled(cron = "0 0 3 * * ?")
- public void job1() {
- System.out.println(“任务进行中。。。”);
- }
- }
- 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:tx="http://www.springframework.org/schema/tx"
- xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
- default-lazy-init="false">
- <context:annotation-config />
- <!—spring扫描注解的配置 -->
- <context:component-scan base-package="com.gy.mytask" />
- <!—开启这个配置,spring才能识别@Scheduled注解 -->
- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
- <task:scheduler id="qbScheduler" pool-size="10"/>
Spring 3.0以后版本的定时任务的更多相关文章
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代
在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...
- SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本
1.项目基础 项目是基于Spring Boot2.x版本的 2.添加依赖 <!-- quartz依赖 --> <dependency> <groupId>org.s ...
- Spring Boot2.0 静态资源被拦截问题
在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...
- spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析
spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例
gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例
先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...
随机推荐
- 很励志的帖子,转来自勉,也反省一下自己写码这几年【奋斗10年,一个.NET程序员从0到拥有5系】
http://bbs.csdn.net/topics/390833230 想想自己毕业近8年,真正写码也5年.从当初毕业时的拒绝写码,到迫不得已开始写码,是命运也好,是自己的不努力也罢.今天看来,写码 ...
- u-boot启动流程分析(1)_平台相关部分
转自:http://www.wowotech.net/u-boot/boot_flow_1.html 1. 前言 本文将结合u-boot的“board—>machine—>arch—> ...
- python 深入理解 赋值、引用、拷贝、作用域
在 python 中赋值语句总是建立对象的引用值,而不是复制对象.因此,python 变量更像是指针,而不是数据存储区域, 这点和大多数 OO 语言类似吧,比如 C++.java 等 ~ 1.先来看个 ...
- ScalaTour 2.函数
/** * 1. case class与模式匹配 */ object TestFunction extends App{ def value(expr:Expr): Int = expr match ...
- oProfile 学习
oProfile工具可以分析CPU的负载量 只要对目标程序加上 -g 后重新编译,即可用oProfile进行分析 例如在测试apache的性能时, 增加 -g 编译选项[crifan@localhos ...
- You need tcl 8.5 or newer in order to run the Redis test
安装Redis时候make test出现的 , 不用下什么tcl8.5再tar了 , 直接yum install tcl就好.
- Bugtags,产品经理的瑞士军刀
做为设计移动应用的产品经理,每天的主要工作就是在手机上不停的体验自己的产品,发现问题.优化体验.你是否经常工作在这样的尴尬场景: 发现界面问题,将问题界面截屏传到电脑,用图片标记工具将问题标记出来,然 ...
- restful api设计中的幂等性的理解。
以前就听说过这个词,但是最近看了一些文章,产生了一些疑惑, GET是幂等性.例如查一条数据,如果你给定一个id.那么你查多少次.都是这条数据,但是我疑问,这里的幂等性指的是,每次都返回一条.这个数量, ...
- vim扩展配置
在用户根目录下新建 “.vimrc”文件,键入一下代码: set syntax=on "高亮语法 set fenc=utf- "设定默认解码 set fencs=utf-,usc- ...
- Liferay中actionURL能够执行后台方法 ,但是页面不跳转问题解决方案
在学习liferay的过程中,遇到了这么一个问题,actionURL能够执行后台方法 ,但是页面不跳转,以下是两种解决方案: 方案1(不推荐此种办法): 强制将页面重定向. 在actionURL执行的 ...