近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合
spring框架来介绍。
一.分类
- Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
- 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
- Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。
- 作业类需要继承自特定的作业类基类,如Quartz中需要继承自org.springframework.scheduling.quartz.QuartzJobBean;java.util.Timer中需要继承自java.util.TimerTask。
- 作业类即普通的java类,不需要继承自任何基类。
注:个人推荐使用第二种方式,因为这样所以的类都是普通类,不需要事先区别对待。
- 每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
- 每到指定时间则触发一次,在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