一:基于xml配置的方式

1:编写普通的pojo 类

package com.aflyun.web.task;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; @Component
//@Service 都可以 
public class TaskCool {
   /**
    * 第一个定时器测试方法
    */
   public void testJob(){
       System.out.println("test first taskJob .... ");
   }
}

2:配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx" 
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:task="http://www.springframework.org/schema/task"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd">    <context:component-scan base-package="com.aflyun.web" />
   <aop:aspectj-autoproxy proxy-target-class="true" />
   <context:annotation-config />
   <!-- 在applicationContext.xml中进行配置,使用定时器
       ref : pojo类的名称
       method : 调用的方式名称
       cron : cronExpression表达式
       cron="0/5 * * * * ?"  //表示五秒钟执行一次
    -->
   <task:scheduled-tasks>
       <task:scheduled ref="taskCool" method="testJob" cron="0/5 * * * * ?"/>
   </task:scheduled-tasks> </beans>

注:上面主要的配置文件中一定要加入task的命名空间和schema。

上面 ref=”taskCool”,默认为这个TaskCool 类的首字母小写的值,若需要修改可以在@Component里面进行修改 ,例如下面 
@Component(“taskCoolJob”) 则此时 ref=”taskCoolJon”。 
到此基于xml配置完成,运行则可以看到效果!

二:基于注解方式

使用注解方式不需要再每写一个任务类还要在xml文件中配置下,方便了很多。使用Spring的@Scheduled,下面先看一注解@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();  
}

cron:表示指定cron表达式。(cron类型表示 是指定时间触发器触发任务执行!)

  • fixedDelay:表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

  • fixedRate:表示从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

下面进行一下具体的配置过程:

1:编写pojo类

package com.tclshop.cms.center.web.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class WebTask {    // 每五秒执行一次
   @Scheduled(cron = "0/5 * * * * ?")
   public void TaskJob() {
       System.out.println("test second annotation style ...");
   }
}

2:配置xml文件

下面贴出相关的配置文件内容:

<!-- 开启这个配置,spring才能识别@Scheduled注解   -->  
   <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
   <task:scheduler id="qbScheduler" pool-size="10"/>

注:理论上只需要加上这句配置就可以了,其他参数都不是必须的。 
配置完成,运行就能看到效果!

总结:这种定时器的使用,不需要集成其他父类定时器,使用简单方便!功能也很强大!


附:cronExpression的配置说明

字段      允许值     允许的特殊字符
秒       0-59        , - * /
分       0-59        , - * /
小时      0-23        , - * /
日期      1-31        , - * ? / L W C
月份      1-12 或者 JAN-DEC     , - * /
星期      1-7 或者 SUN-SAT      , - * ? / L C #
年(可选)       留空, 1970-2099       , - * /

例子:

CRON表达式         含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发

三:参考资料

1:Spring定时任务的几种实现 :

http://gong1208.iteye.com/blog/1773177

2:Spring中任务调度cronExpression配置说明:

http://www.cnblogs.com/xiaopeng84/archive/2009/11/26/1611427.html

3:QuartZ Cron表达式

http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html

spring的定时器的更多相关文章

  1. Spring任务调度定时器

    1.在spring-context.xml配置 <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ...

  2. SSH框架中 Spring设置定时器 Quartz

    一,首先下载quartz-1.6.0.jar架包,到lib目录下 二,写你自己定时器业务方法 package com.lbnet.lzx.timing; import org.quartz.JobEx ...

  3. spring task定时器笔记

    定时器有两种方式 1.延迟启动 <bean id="timerTaskRunnerChain" class="bingo.uam.task.TimerTaskRun ...

  4. Spring注解定时器使用

    一.首先要配置我们的spring-service.xml 1.xmlns 多加下面的内容 xmlns:task="http://www.springframework.org/schema/ ...

  5. 项目总结08:spring quartz 定时器Demo

    将定时器用到的quartz.jar放在lip文件下 quartz.xml文件(完整) <?xml version="1.0" encoding="UTF-8&quo ...

  6. Spring设置定时器配置

    corn表达式生成:http://www.pppet.net/ 1.注解方式 添加命名空间 xmlns:task="http://www.springframework.org/schema ...

  7. 分享Spring Scheduled定时器的用法

    摘要:在coding中经常会用到定时器,指定每隔1个小时,或是每天凌晨2点执行一段代码段,若是使用java.util.Timer来做这种事情,未免重复造轮子.幸亏Spring中封装有定时器,而且非常好 ...

  8. spring task定时器的配置使用

    spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...

  9. 分享知识-快乐自己:Spring整合定时器

    前期工作:(引入相关 JAR ) <spring.quartz>1.8.4</spring.quartz> <!--spring 定时--> <depende ...

随机推荐

  1. iOS 仿抖音 视频裁剪

    1.最近做短视频拍摄.其中的裁剪界面要做得和抖音的视频裁剪效果一样 需求:  裁剪有一个最大裁剪时间.最小裁剪时间.左右拖动可以实时查看对应的视频画面.拖动进度条也能查看对应的画面 .拖动底部视图也能 ...

  2. Widows自带系统监控工具——24小时监控服务器性能

    博文来源:https://blog.csdn.net/qq_41650233/article/details/84313153 操作步骤1.运行程序perfmon.exe 2.在数据收集器下选择[用户 ...

  3. unity中调试模型时unity崩溃问题

    这个问题是在我调试3D模型资源时出现的,每当在Scene场景中调试模型时unity崩溃,出现Unity Bug Reporter页面,反复出现这个问题,很烧脑 对于这个问题我表示很无语,但是经过不断查 ...

  4. webpack 学习小结

    webpack 是一个模块打包工具(前提要安装 node使用npm来安装webpack) 1.安装webpack,webpack-cli , webpack-dev-server //全局安装 npm ...

  5. C++智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

  6. JS中this指向问题相关知识点及解析

    概括:this指向在函数定义的时候是无法确定的,只有在函数调用执行的时候才能确定this最终指向了谁,this最终指向的是调用它的对象(常见的说法,后面有小小的纠正): 例1: 图中的函数fn1其实是 ...

  7. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  8. mysql分库分表,做到永不迁移数据和避免热点

    作者:老顾聊技术   搜云库技术团队  来源:https://www.toutiao.com/i6677459303055491597 一.前言 中大型项目中,一旦遇到数据量比较大,小伙伴应该都知道就 ...

  9. 机器学习之--线性回归sigmoid函数分类

    import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import random #sigmoid函数 ...

  10. vue-cli3.0使用及配置

    1.先全局安装vue-cli3.0 检测安装: vue -V 2.创建项目(这个就跟react创建脚手架项目比较像了)   这里如果你是第一次用3.0版本的话,是没有前两个的,而只有最后两个,这里是 ...