spring提供了定时任务功能。我们不需要第三者jar包支持。spring够了。

代码:

package com.inth.product.web.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import com.inth.product.service.impl.ContractServiceImpl; @Component("changeStateTask")
public class ChangeStateTask{ @Autowired
private ContractServiceImpl contractServiceImpl;
/**
* 定时更改合同状态
* 0 0 * * * ? 表示每次秒和分为0时触发一次(每小时一次)
* "@Scheduled"时spring提供的定时任务标签
* 须要在applicationContext.xml中加入
* xmlns:task="http://www.springframework.org/schema/task"
* xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "
* <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
* <task:scheduler id="qbScheduler" pool-size="10"/
* ChangeStateTask.doJob()<BR>
* <P>Author : DingYinLong </P>
* <P>Date : 2014年7月28日 </P>
*/
@Scheduled(cron = "0 0 * * * ?")
public void doJob(){
this.contractServiceImpl.executeStateChange();
}
/**
* 固定每分钟运行一次
* ChangeStateTask.doJob1()<BR>
* <P>Author : DingYinLong </P>
* <P>Date : 2014年8月1日 </P>
*/
@Scheduled(fixedRate = 60*1000)
public void doJob1(){
System.out.println(new Date() + "-----------------doJob1");
}
/**
* 上次任务结束后一分钟后再次运行
* ChangeStateTask.doJob2()<BR>
* <P>Author : DingYinLong </P>
* <P>Date : 2014年8月1日 </P>
*/
@Scheduled(fixedDelay = 60*1000)
public void doJob2(){
System.out.println(new Date() + "-----------------doJob2");
}
}

applicationContext.xml配置文件:

<?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:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
<!-- 扫描包基础文件夹 -->
<context:component-scan base-package="com.inth" />
<!-- 识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

注意事项:

1,beans 属性加上xmlns:task="http://www.springframework.org/schema/task"以及xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "

2,fixedRate和fixedDelay的差别写在凝视上了。

以上情况不基于注解纯配置例如以下:

代码:

package com.inth.product.web.task;

import java.util.Date;

import com.inth.product.service.impl.ContractServiceImpl;

public class ChangeStateTask{
private ContractServiceImpl contractServiceImpl;
public void doJob(){
System.out.println(new Date() + "-----------------doJob");
// this.contractServiceImpl.executeStateChange();
}
public void doJob1(){
System.out.println(new Date() + "-----------------doJob1");
}
public void doJob2(){
System.out.println(new Date() + "-----------------doJob2");
}
}

applicationContext.xml配置:

<?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:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ">
<!-- 扫描包基础文件夹 -->
<context:component-scan base-package="com.inth" />
<bean name="taskJob" class="com.inth.product.web.task.ChangeStateTask"></bean>
<task:scheduled-tasks>
<task:scheduled ref="taskJob" method="doJob" cron="0/5 * * * * ? "/>
<task:scheduled ref="taskJob" method="doJob1" fixed-rate="5000"/>
<task:scheduled ref="taskJob" method="doJob2" fixed-delay="5000"/>
</task:scheduled-tasks>
</beans>

附:cron表达式配置说明 





字段 同意值 同意的特殊字符 

秒 0-59 , - * / 

分 0-59 , - * / 

小时 0-23 , - * / 

日期 1-31 , - * ?

/ L W C 

月份 1-12 或者 JAN-DEC , - * / 

星期 1-7 或者 SUN-SAT , - * ?

/ L C # 

年(可选) 留空, 1970-2099 , - * / 

表达式 意义 

"0 0 12 * * ?" 每天中午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期间的每1分钟触发 

"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 

"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 

"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 

"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 

"0 15 10 ?

* MON-FRI" 周一至周五的上午10:15触发 

"0 15 10 15 * ?

" 每月15日上午10:15触发 

"0 15 10 L * ?" 每月最后一日的上午10:15触发 

"0 15 10 ?

* 6L" 每月的最后一个星期五上午10:15触发 

"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 

"0 15 10 ?

* 6#3" 每月的第三个星期五上午10:15触发 

特殊字符 意义 

* 表示全部值。 

?

表示未说明的值,即不关心它为何值。 

- 表示一个指定的范围; 

, 表示附加一个可能值; 

/ 符号前表示開始时间,符号的每个增量后值;

版权声明:本文博主原创文章。博客,未经同意不得转载。

spring常规任务(轻便易)的更多相关文章

  1. spring注解和xml方式区别详解

    一.spring常规方式. 在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立.下面是 3 个类,它们分别是 Office.Car 和 Boss,这 3 ...

  2. Spring实战1:Spring初探

    主要内容 Spring的使命--简化Java开发 Spring容器 Spring的整体架构 Spring的新发展 现在的Java程序员赶上了好时候.在将近20年的历史中,Java的发展历经沉浮.尽管有 ...

  3. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

  4. 使用XFire+Spring构建Web Service(一)——helloWorld篇

    转自:http://www.blogjava.net/amigoxie/archive/2007/09/26/148207.html原文出处:http://tech.it168.com/j/2007- ...

  5. spring mvc 经典总结

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  6. 使用XFire+Spring构建Web Service

    XFire是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地开发Web Service应用. 相 对于Axis来说,目 ...

  7. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  8. 使用 Spring 2.5 基于注解驱动的 Spring MVC--转

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  9. 想要快速上手 Spring Boot?看这些教程就足够了!

    1.项目名称:分布式敏捷开发系统架构 项目简介:基于 Spring + SpringMVC + Mybatis 分布式敏捷开发系统架构,提供整套公共微服务服务模块:集中权限管理(单点登录).内容管理. ...

随机推荐

  1. WPF案例 (五) 对控件界面使用倒影

    原文:WPF案例 (五) 对控件界面使用倒影 在这个程序里对5个2D控件界面应用了垂直倒影,边缘模糊化和模型变换,在本例中,这5个2D控件为Border, 各包含了一幅Image,界面如下图所示,源码 ...

  2. 一 手游开发工具cocos2d-x editor初识

    可学习的demo: 7个实战项目 flappybird(飞扬小鸟).popstar(消灭星星).fruitninja(水果忍者).2048(数度消除). moonwarriors(月亮战神).frui ...

  3. iOS_25彩票_幸运转盘

    终于效果图: 各个view的关系图: 背景圆盘(须要扣图处理)LuckyBaseBackground.png 盖在背景圆盘上面的转盘 LuckyRotateWheel.png watermark/2/ ...

  4. Android Fragment使用

                     通常地 fragment做为宿主activity UI的一部分, 被作为activity整个view hierarchy的一部分被嵌入. 有2种方法你能够加入一个fr ...

  5. WampServer:轻松配置Wordpress安装环境

    WordPress运行环境 PHP 5.2.4 或更新版本(不支持第三方推出的“PHP 6.0”) MySQL 5.0 或更新版本 Apache mod_rewrite 模块(可选,用于支持“固定链接 ...

  6. C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象

    重载操作符与转换 --调用操作符和函数对象 引言: 能够为类类型的对象重载函数调用操作符:一般为表示操作的类重载调用操作符! struct absInt { int operator() (int v ...

  7. java基金会成立Set

    1.设置 当向集合Set中添加对象时.首先集合计算要添加对象的hashcode,依据该值得到一个位置用来存放当前的对象,当在该位置没有一个对象存在的时候,集合set觉得该对象在集合中不存在,直接添加进 ...

  8. 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数

    Ready 事件不一定 ready 使用 easyloader 的时候,必须要注意到脚本的加载时机问题,easyloader 会异步加载模块,所以,你使用的模块不一定已经加载了.比如下面的代码. &l ...

  9. 第一章_servlet

    [Servlet与Jsp学习指南] *学习这servlet前.需要下载servlet-api.jar,MyEclipse给自己带来的需求javaEE3.0的天赋足以使用注解获得的版本号servlet ...

  10. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...