• Spring注解实现:Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz。
  1. maven配置:

    <!--quartz-->
    <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
    </dependency>
    <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.1</version>
    </dependency>
  2. 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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd ">
    <!--启用注解,加载定时任务-->
    <task:annotation-driven />
    <context:annotation-config />
    <!--定时任务扫描的包-->
    <context:component-scan base-package="*.*.*"/>
    </beans>
  3. 实现:
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component; @Component
    public class TimeTest { @Scheduled(cron="*/3 * * * * ?")//每10秒跑一次任务
    public void tesTime(){
    System.out.println("定时器执行成功!---------66666----------");
    }
    }


  • 配置Quartz实现定时器功能:

    • 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描包 -->
<context:component-scan base-package="com.*.*" /> <!-- 要调用的工作类 -->
<bean id="timeTestBean" class="com.*.*.TimeTest"></bean> <!-- 定义调用对象和调用对象的方法 -->
<bean id="job" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="timeTestBean"></property>
<!-- 调用类中的方法 -->
<property name="targetMethod" value="tesTime"></property>
</bean> <!-- 定义触发时间 -->
<bean id="time" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="job"></property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0/2 * * * * ?</value><!-- 每2秒执行一次 -->
</property>
</bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<!-- lazy-init="false" autowire="no" -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="time" />
</list>
</property>
</bean>
</beans>


  • 时间配置:
示例如下:
0 0 12 * * ?---------------在每天中午12:00触发
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:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和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, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发

Spring注解实现定时功能以及Quartz定时器的更多相关文章

  1. spring jar 包 用处功能:

    自己积累的: @   spring-context-3.0.5.RELEASE.jar :主要用于 spring程序中加载类 ApplicationContext 用.eq: ApplicationC ...

  2. Spring定时器,定时执行(quartz)

    这个定时器与继承了timertask的定时器不同的是,这个定时器是更强大的,可以指定每分的第n秒,每天的第n时,每周的.每年的.来定时运行这个定时器.那么下面来讲诉如何使用quartz定时器. spr ...

  3. java Quartz定时器任务与Spring task定时的几种实现,

    java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置    请查阅   http://www.cnblogs.com/si ...

  4. quartz实现定时功能实例详解(servlet定时器配置方法)

    Quartz是一个完全由java编写的开源作业调度框架,下面提供一个小例子供大家参考,还有在servlet配置的方法 Quartz是一个完全由java编写的开源作业调度框架,具体的介绍可到http:/ ...

  5. spring和Quartz的定时功能

    一:前沿 最近在做一个定时的功能,就是在一定时间内查询订单,然后告诉用户未付款,已付款等消息通知,而且要做集群的功能,这个集群的功能是指,我部署两套代码,其中一个定时的功能在运行,另外一个就不要运行. ...

  6. java定时器,Spring定时器和Quartz定时器

    一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...

  7. 基于spring和Quartz定时器

    最近做一个小项目,要每7天去调用webservice获取一次数据.所以就用定时器来完成spring是4.1.6,quartz是2.2.1. 首先配置spring的xml文件.首先定义你要被执行的类 & ...

  8. 启用Spring quartz定时器,导致tomcat服务器自动停止

    在项目中添加了一个定时功能,基于Spring quartz: 设置好执行时间后(如:每天14:00) 当程序执行完后,就会出现以下信息: 2013-7-22 11:36:02 org.apache.c ...

  9. spring启动quartz定时器

    在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...

随机推荐

  1. js中的执行环境和作用域链

    首先介绍一些即将用到的概念: 执行环境:  它定义了变量和函数有权访问其他数据的范围,每一个执行环境都有一个与之关联的变量对象,环境中定义的所有变量和函数都保存在这个变量对象中.   所有javasc ...

  2. Spring MVC F5刷新问题

    转自:https://bbs.csdn.net/topics/390771056 post操作成功后重定向到B,这样浏览器里F5的时候就不会让提交A了    

  3. Xshell的简单使用

    1.下载并安装 Xshell 4打开后如下图所示,会出现一个界面框,这个界面框类似于DOS的界面,需要操控远程的主机,都是通过这个界面进行操作. 2在这个界面左上角的位置有一个文件按钮,点击这个按钮. ...

  4. 新创建的maven项目,显示的jdk版本与使用的不一致

    解决:是在安装的maven中的setting.xml配置文件中添加 在setting.xml配置文件中的<profiles></profiles>这个元素中加以下代码 如果加上 ...

  5. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  6. iOS UITableView制作类似QQ好友列表视图

                #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDele ...

  7. HDOJ 4802 GPA

    Problem Description In college, a student may take several courses. for each course i, he earns a ce ...

  8. 【Boost】boost库获取格式化时间

    获取时间方式 格式一:YYYYMMDD #include<iostream> #include<string> #include<boost/date_time/greg ...

  9. 高性能MySQL笔记-第5章Indexing for High Performance-003索引的作用

    一. 1. 1). Indexes reduce the amount of data the server has to examine.2). Indexes help the server av ...

  10. Person的delete请求--------详细过程

    首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...