Spring定时任务的几种实现


近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合

spring框架来介绍。

一.分类

  • 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品):

  1. Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
  2. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
  3. Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。
  • 从作业类的继承方式来讲,可以分为两类:

  1. 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要继承自java.util.TimerTask。
  2. 作业类即普通的java类,不需要继承自任何基类。

注:个人推荐使用第二种方式,因为这样所以的类都是普通类,不需要事先区别对待。

 

  • 从任务调度的触发时机来分,这里主要是针对作业使用的触发器,主要有以下两种:
  1. 每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
  2. 每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

注:并非每种任务都可以使用这两种触发器,如java.util.TimerTask任务就只能使用第一种。Quartz和spring task都可以支持这两种触发条件。


<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"  
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
	xsi:schemaLocation="   
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
	<!-- 声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类,targetMethod指定运行的方法-->
	<bean id="alarmInfoJob"  
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
		<property name="targetObject">  
			<bean class="com.gense.activity.weixin.WeixinSendAlermInfo" />  
		</property>  
		<property name="targetMethod" value="execute" />  
		<property name="concurrent" value="false" /><!-- 作业不并发调度 -->  
	</bean> 
	
	
	<!-- 第一种alarmInfoTrigger,只支持按照一定频度调用任务,如每隔30分钟运行一次 -->
	<bean id="alarmInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
		<property name="jobDetail" ref="alarmInfoJob" />  
		<property name="startDelay" value="0" /> <!--调度工厂实例化后,经过0秒开始执行调度 -->  
		<property name="repeatInterval" value="1*60*1000" /><!-- 每1分调度一次 -->  
	</bean>
	
	
	<!-- 第二种cronAlarmInfoTrigger,支持到指定时间运行一次,如每天12:00运行一次等。 -->
	<bean id="cronAlarmInfoTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="alarmInfoJob" />
		<property name="cronExpression">
			<value>0 30 23 * * ?</value>
		</property>
	</bean>
	
	<!-- 配置调度工厂  以上2种可任选一种,根据自己情况而定 -->
	<bean id="schedulerAttCountFactory"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<!-- 这里选择第一种 频繁 调度任务 -->
				<ref local="alarmInfoTrigger"/>
			</list>
		</property>
	</bean>
</beans></span>


相关连接

http://blog.csdn.net/rockstar541/article/details/21093261



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值