前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring中定时器实现

Spring中定时器实现

作者头像
Java团长
发布2018-08-03 15:47:50
8010
发布2018-08-03 15:47:50
举报

在一些工作需要使用到定时器,Spring很好的集成了定时器的功能!

在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,下面介绍两种方式实现Spring定时器功能,一种是基于xml配置方式,另外一种是基于注解的方式,大家根据自己的项目选择适合自己的。

一:基于xml配置的方式

1:编写普通的pojo 类
代码语言:javascript
复制
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文件
代码语言:javascript
复制
<?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在源文件中的定义:

代码语言:javascript
复制
@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类
代码语言:javascript
复制
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文件

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

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

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

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


附:cronExpression的配置说明

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

例子:

代码语言:javascript
复制
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

  • 原文链接:blog.csdn.net/u010648555/article/details/52162840
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java团长 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一:基于xml配置的方式
    • 1:编写普通的pojo 类
      • 2:配置xml文件
      • 二:基于注解方式
        • 1:编写pojo类
          • 2:配置xml文件
          • 三:参考资料
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档