1定时器的作用
在实际的开发中,如果项目中需要定时执行或者需要重复执行一定的工作,定时器显现的尤为重要。
当然如果我们不了解定时器就会用线程去实现,例如:
package org.lzstone.action
public class financeaction extends thread{
private date date;
public void run{
try{
while(true){
thread.sleep((int)(math.random()*1000));
date = new date();
//定时执行任务
}
}catch(exception e){
e.printstacktrace();
}
}
}
自己实现定时器的工作很复杂,如果实现不好占用内存过多,系统就此over,所以处理定时执行或者重复执行的任务,定时器是很好的选择
2.java中常见的定时器
1)借助java.util.timer来实现
2)opensymphony社区提供的quartz来实现
3.介绍timer
利用timer开发定时任务是主要分为两个步骤:
1)创建定时任务类
示例代码:
package org.lzstone.action
import java.util.timetask
public class lzstonetimetask extends timetask{
public void run(){
//执行的定时器任务
}
}
2)运行定时任务,运行定时任务分为两种方式:
2.1)程序直接启动
示例代码:
package org.lzstone.action
public class lzstonemain{
.......
public void run(){
//执行定时器的任务
//创建实例
timer timer = new timer();
参数:
new lzstonetimetask()- 所要安排的任务。
0- 执行任务前的延迟时间,单位是毫秒。
1*1000- 执行各后续任务之间的时间间隔,单位是毫秒。
timer.schedule(new lzstonetimetask(),0,1*1000);
}
}
2.2)web监听方式
示例代码:
package org.lzstone.action
public class lzstonemain implements servletcontextlistener{
private timer timer = null;
//初始化监听器,创建实例,执行任务
public void contextinitialized(servletcontextevent event){
timer = new timer();
timer.schedule(new lzstonetimetask(),0,1*1000);
}
//销毁监听器,停止执行任务
public void contextdestroyed(servletcontextevent event){
//注意,在此计时器调用的计时器任务的 run 方法内调用此方法,就可以绝对确保正在执行的任务是此计时器所执行的最后一个任务。
timer.cancel();
}
}
web.xml配置
<listener>
<listener-class>
org.lzstone.action.lzstonemain
</listener-class>
</listener>
4. 介绍quartz
quartz是opensymphony开源组织在job scheduling领域又一个开源项目,可以用来创建简单或者复杂的定时任务,利用quartz开发定时任务的步骤与timer类
似。
利用quartz开发定时任务是主要分为两个步骤:
1)创建定时任务类
示例代码:
package org.lzstone.action
public class lzstonetimetask implements job{
public void execute(jobexecutioncontext context) throws jobexecutionexception{
//执行的定时器任务
}
}
2)运行定时任务,运行定时任务分为两种方式:
2.1)程序直接启动,创建任务调度器及配置相应的任务计划
示例代码:
package org.lzstone.action
public class lzstonemain{
private static scheduler sched;
public static void run() throws exception{
//创建lzstonetimetask的定时任务
jobdetail jobdetail = new jobdetail(lzstonejob,sched.default_group,lzstonetimetask.class);
//目标 创建任务计划
crontrigger trigger = new crontrigger(lzstonetrigger,lzstone,0 0 12 * * ?);
//0 0 12 * * ? 代表每天的中午12点触发
sched = new org.quartz.impl.stdschedulerfactory().getscheduler();
sched.schedulejob(jobdetail,trigger);
sched.start();
}
//停止
public static void stop() throws exception{
sched.shutdown();
}
}
//执行
public class main{
.............
public void run(){
lzstonemain.run();
}
............
}
2.2)web监听方式
示例代码:
package org.lzstone.action
public class lzstonemainlistener implements servletcontextlistener{
private timer timer = null;
//初始化监听器,创建实例,执行任务
public void contextinitialized(servletcontextevent event){
lzstonemain.run();
}
//销毁监听器,停止执行任务
public void contextdestroyed(servletcontextevent event){
lzstonemain.stop();
}
}
web.xml配置
<listener>
<listener-class>
org.lzstone.action.lzstonemainlistener
</listener-class>
</listener>
5.对比
timer方式实现定时器,原理简单,实现方便,在执行简单的任务比较方便,不足之处是无法确定执行时间,并且依赖性比较强,必须继承指定的类
quartz方式实现定时器,方便,清晰指定启动时间,定时参数比较灵活,容易实现比较复杂的定时任务,不足之处是需要实现特定接口,加载其框架
两种方式各有优缺点,在特定场合可以根据其特点选择使用。
6.spring定时任务
spring定时任务对timer与quartz都提供了支持,并且实现步骤基本一样
首先配置spring对timer的支持
1.1 创建定时任务类
package org.lzstone.action
import java.util.timetask
public class lzstonetimetask extends timetask{
public void run(){
//执行的定时器任务
}
}
1.2 注册定时任务类,配置任务计划与任务调度器
在项目的web-inf下面创建timerconfig.xml文件(一般叫做applicationcontext.xml)
<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean>
<!--注册定时执行任务实体-->
<bean id="lzstonetimetask" class="org.lzstone.action.lzstonetimetask"/>
<!--注册定时器信息-->
<bean id="taskinfo" class="org.springframework.scheduling.timer.scheduledtimertask">
<!--第一次执行任务前需要等待的时间,这里设置为3秒-->
<property name="delay">
<value>3000</value>
</property>
<!--设置任务的执行周期 这里设置为4秒-->
<property name="period">
<value>4000</value>
</property>
<!--设置具体执行的任务 这里设置为lzstonetimetask-->
<property name="timertask">
<ref local="lzstonetimetask"/>
</property>
</bean>
<!--配置定时器任务的调度器-->
<bean id="timerfactory" class="org.springframework.scheduling.timer.timerfactorybean">
<!--注册定时器列表-->
<property name="scheduledtimertasks">
<list>
<ref local="taskinfo"/>
........
</list>
</property>
</bean>
</beans>
1.3 web项目中的启动设置
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/timerconfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.contextloaderlistener
</listener-class>
</listener>
配置spring对quartz的支持
2.1 创建定时任务类
package org.lzstone.action
public class lzstonequartztask{
public void execute(){
//执行的定时器任务
}
}
2.2 注册定时任务类,配置任务计划与任务调度器
在项目的web-inf下面创建quartzconfig.xml文件(一般叫做applicationcontext.xml)
<?xml version="1.0" encoding="utf-8"?>
<!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean>
<!--注册定时执行任务实体-->
<bean id="lzstonequartztask" class="org.lzstone.action.lzstonequartztask"/>
<!--注册定时器信息-->
<bean id="taskinfo" class="org.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean">
<!--指定要执行的定时任务类 这里是lzstonequartztask-->
<property name="targetobject">
<ref local="lzstonequartztask"/>
</property>
<!--指定定时器任务类要执行的方法名称 这里是execute-->
<property name="targetmethod">
<value>execute</value>
</property>
</bean>
<!--配置定时器任务的调度器-->
<bean id="quartztrigger" class="org.springframework.scheduling.quartz.crontriggerbean">
<!--声明要运行的实体-->
<property name="jobdetail">
<ref local="taskinfo"/>
</property>
<!--设置运行时间-->
<property name="cronexpression">
<value>0 0 12 * * ?</value>
</property>
</bean>
<!--注册监听器-->
<bean id="registerquartz" class="org.springframework.scheduling.quartz.schedulerfactorybean">
<!--注册定时器实体 集合-->
<property name="triggers">
<list>
<ref local="quartztrigger"/>
</list>
</property>
</bean>
</beans>
2.3 web项目中的启动设置
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/quartzconfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.contextloaderlistener
</listener-class>
</listener>
更多定时器的实现、java定时器timer和quartz介绍与spring中定时器的配置。