spring quartz 定时器

说到定时器,在现实生活肯定有很多地方都需要的,比如,股票的刷新啦、手机app里的天气状态啦等等。万能的java肯定也是可以实现定时器功能的,但是,纯粹的java定时器,是有点缺陷的,比如,不能随着服务器的部署或者重启而自动启动,而且,如果是复杂的逻辑,比如我只希望工作日执行,周末就不执行,这样的case使用java定时器就不能实现,或者很难实现了。所以,这里就使用一个叫quartz的定时器。



我的环境是

JDK1.7

Tomcat 7

quartz 1.8.5

spring 3.0.2


实现步骤

1.编写自己的任务类

因为只是简单示例,就写的很简单了

package com.zy.timer;


public class HotWaterTask{

	private static Long lastTime = System.currentTimeMillis();
	public void execute(){
		Long nowTime = System.currentTimeMillis();
		
		System.out.println("定时器执行,间隔" + (nowTime - lastTime )+"毫秒");
		
		lastTime = nowTime;
	}

}

2.添加需要的spring的jar和quartz的jar

本程序需要的jar包下载,请戳我


3.指定加载spring文件

因为我们是web项目,所以,我们就在web.xm里配置spring的xml文件位置

	<!-- web中配置配置文件地址和类加载监听器 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	   
	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


4.配置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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 自定义的任务类 -->
	<bean id="qtzJob" class="com.zy.timer.HotWaterTask"/>
	<!-- 任务类的执行方法 -->
	<bean id="qtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
	    <property name="targetObject">  
	        <ref bean="qtzJob"/>  		<!-- 要执行的任务类 -->
	    </property>  
	    <property name="targetMethod">  <!-- 要执行的方法名称 -->  
	        <value>execute</value>  
	    </property>  
	</bean> 
	
	<!-- ======================== 调度触发器 ======================== -->  
	<bean id="qtzJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
	    <property name="jobDetail" ref="qtzJobMethod"></property>  			<!-- 指定调用任务类的执行方法 -->
	    <property name="cronExpression" value="0/5 * * * * ?"></property>   <!-- 调用的时机   这里是每隔五秒调用一次-->
	</bean>  

	<!-- ======================== 调度工厂 ======================== -->  
	<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
	    <property name="triggers">  			<!-- 触发器集合,可以放入多个触发器 -->
	        <list>  
	            <ref bean="qtzJobTrigger"/> 	<!-- 上面我们指定好的触发器 --> 
	        </list>  
	    </property>  
	</bean>    
</beans>

5.运行效果

因为机器的配置或者内存使用等情况,无法精确到毫秒,但是误差在几毫秒之内,完全是我们能接受的了!




java定时器、spring定时器、quartz定时器的比较和分别实现

quartz参数详细


j2se方式的使用quartz


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值