spring执行定时任务

定义个任务是很简单实现TimerTaskrun思路方法就可以了.
如下:SayHelloTask.java
package test.timerTask;

import java.util.TimerTask;

public  SayHelloTask extends TimerTask {

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

}

然后是配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
<beans>
<bean id="sayHelloTask" ="test.timerTask.SayHelloTask"></bean>
<bean id="scheduledTask" ="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" ="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  TestApp {

  /**
   * @param args
   */
  public  void (String args) {
    // TODO Auto-generated method stub
    ApplicationContext context =  ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml");
 //   ApplicationContext context2 =  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  SayHelloTaskUsingQuartz extends QuartzJobBean {

  @Override
  protected void executeInternal(JobExecutionContext context)
      throws JobExecutionException {
    // TODO Auto-generated method stub
    .out.prln("使用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" ="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>test.timerTask.SayHelloTaskUsingQuartz</value>
</property>
</bean>
<!-- 关键在如下两个触发器配置 -->
<!-- 类似于Java简单触发器 -->

<bean id="helloTrigger" ="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" ="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" ="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<ref bean="helloCronTrigger"/>
</property>
</bean>
</beans>
有关简单触发器和复杂触发器,查考下面解释:
Quartz设计者做了个设计选择来从调度分离开作业Quartz中触发器用来告诉调度作业什么时候触发框架提供了把触发器类型但两个最常用是SimpleTrigger和CronTriggerSimpleTrigger为需要简单打火调度而设计典型地如果你需要在给定时间和重复次数或者两次打火的间等待秒数打火个作业那么SimpleTrigger适合你方面如果你有许多复杂作业调度那么或许需要CronTrigger

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

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

下面表达式
0 15 10 ? * 6L 2002-2005
将在2002年到2005年每个月最后个星期 5上午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 specy 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 specy 'no specic value'. This is useful when you need to specy something in one of the two fileds, but not the other. See the examples below for clarication.
“?”只在日期域和星期域中使用它被用来指定“非明确值”当你需要通过在这两个域中个来指定些东西时候它是有用看下面例子你就会明白
The '-' character is used to specy 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 specy 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”在星期域里表示”星期、星期 3、星期 5”.








Tags: 

延伸阅读

最新评论

发表评论