<%@ page contentType="text/html; charset=gb2312"%> spring执行定时任务
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
spring执行定时任务
     发布者: 发布时间:2006-09-25

定义一个任务是很简单的实现TimerTask的run方法就可以了.


 

如下:SayHelloTask.java


 

package test.timerTask;

import java.util.TimerTask;

public class SayHelloTask extends TimerTask {

  @Override
  public void run() {
    // TODO Auto-generated method stub
    System.out.println("测试TimerTask : Hello !!");
  }

}


 

然后是配置文件:


 

<?xml version="1.0" encoding="UTF-8"?>


 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >


 

<beans>


 

<bean id="sayHelloTask" class="test.timerTask.SayHelloTask"></bean>


 

<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">


 

<property name="timerTask">


 

<ref bean="sayHelloTask"/>


 

</property>


 

<!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档-->


 

<property name="period">


 

<value>2000</value>


 

</property>


 

<!-- 延时1m 执行任务 -->


 

<property name="delay">


 

<value>1000</value>


 

</property>


 

</bean>


 


 

<!-- 启动定时器 -->


 

<bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean">


 

<property name="scheduledTimerTasks">


 

<list>


 

<ref bean="scheduledTask"/>


 

</list>


 

</property>


 

</bean>


 

</beans>


 

测试类如下:TestApp.java


 

package test.timerTask;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

  /**
   @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext context = new ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml");
 //   ApplicationContext context2 = new ClassPathXmlApplicationContext("test/timerTask/quartzTimer.xml");
  }
// 只要加载配置文件就可以了,
}


 

使用Java中的定时器比较简单,其提供的任务也比较简单, 下面来看看使用quartz来执行一个复杂的任务.


 

首先制定一个任务, 实现QuartzJobBean中的方法.


 

package test.timerTask;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class SayHelloTaskUsingQuartz extends QuartzJobBean {

  @Override
  protected void executeInternal(JobExecutionContext context)
      throws JobExecutionException {
    // TODO Auto-generated method stub
    System.out.println("使用Quartz 认为调度: Hello!!");
  }

}


 

配置代码如下:quartzTimer.xml


 

<?xml version="1.0" encoding="UTF-8"?>


 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >


 

<beans>


 

<bean id="sayHelloJob" class="org.springframework.scheduling.quartz.JobDetailBean">


 

<property name="jobClass">


 

<value>test.timerTask.SayHelloTaskUsingQuartz</value>


 

</property>


 

</bean>


 

<!-- 关键在如下两个触发器的配置 -->


 

<!-- 类似于Java的简单触发器 -->


 


 

<bean id="helloTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">


 

<property name="jobDetail">


 

<ref bean="sayHelloJob"/>


 

</property>


 

<property name="startDelay">


 

<value>1000</value>


 

</property>


 

<property name="repeatInterval">


 

<value>3000</value>


 

</property>


 

</bean>


 

<!-- 复杂触发器 -->


 


 

<bean id="helloCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">


 

<property name="jobDetail">


 

<ref bean="sayHelloJob"/>


 

</property>


 

<property name="cronExpression">


 

<!-- 关键在配置此表达式 -->


 

<value>0 49 15 * * ?</value>


 

</property>


 


 

</bean>


 

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">


 

<property name="triggers">


 

<ref bean="helloCronTrigger"/>


 

</property>


 

</bean>


 

</beans>


 

关于简单触发器和复杂触发器,查考下面的解释:


 

Quartz设计者做了一个设计选择来从调度分离开作业。Quartz中的触发器用来告诉调度程序作业什么时候触发。框架提供了一把触发器类型,但两个最常用的是SimpleTrigger和CronTrigger。SimpleTrigger为需要简单打火调度而设计。典型地,如果你需要在给定的时间和重复次数或者两次打火之间等待的秒数打火一个作业,那么SimpleTrigger适合你。另一方面,如果你有许多复杂的作业调度,那么或许需要CronTrigger。

CronTrigger是基于Calendar-like调度的。当你需要在除星期六和星期天外的每天上午10点半执行作业时,那么应该使用CronTrigger。正如它的名字所暗示的那样,CronTrigger是基于Unix克隆表达式的。

作为一个例子,下面的Quartz克隆表达式将在星期一到星期五的每天上午10点15分执行一个作业。
0 15 10 ? * MON-FRI

下面的表达式
0 15 10 ? * 6L 2002-2005
将在2002年到2005年的每个月的最后一个星期五上午10点15分执行作业。

你不可能用SimpleTrigger来做这些事情。你可以用两者之中的任何一个,但哪个跟合适则取决于你的调度需要。


 

更多详细介绍参考此处:


 

关于cronExpression的介绍:

字段
  
 允许值
  
 允许的特殊字符

 
 秒
   
 0-59
   
 , - * /

 
 分
   
 0-59
   
 , - * /

 
 小时
   
 0-23
   
 , - * /

 
 日期
   
 1-31
   
 , - * ? / L W C

 
 月份
   
 1-12 或者 JAN-DEC
   
 , - * /

 
 星期
   
 1-7 或者 SUN-SAT
   
 , - * ? / L C #

 
 年(可选)
   
 留空, 1970-2099
   
 , - * / 
 

如上面的表达式所示:


 

详细说明如下:


 

The ´*´ character is used to specify all values. For example, "*" in the minute field means "every minute".


 

“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。


 

The ´?´ character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify ´no specific value´. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.


 

“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。


 

The ´-´ character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".


 

“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。


 

The ´,´ character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day".


 

“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

 

(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
Spring发邮件
Java开源J2EE服务器
信用评分方法
ETL 设计实施策略
在Windows2003 上配置对 SQL Server 2005 Analysis Services 的 HTTP 访
用JNI调用C或C++动态联接库入门
 
最近评论:
        
你曾悄悄的来过!
wow gold,wow gold,wow gold,ffxi gil max(5302)
        
冰封的往事!
wow power leveling,wow gold,WoW Gold,wow gold max(4371)
        
飞舞的传奇!
传世私服,传世私服.传奇世界私服传奇世界私服,传世私服传世私服, 传奇世界私服传奇世界私服.传奇私服传奇私服. max(2107)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号