`
rentianchou
  • 浏览: 68475 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring2.5与Quartz的小例子

阅读更多
applicationContext-quartz.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="false">
 
<!-- task -->
<!-- 定义了一个任务 -->
<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
   <property name="jobClass">
    <value>mrd.shcreating.task.TaskServer</value><!-- 类的名字 有这个类完成具体的 任务 -->
   </property>
</bean>

<!-- 第二步 调度定时任务 -->
<!--这种配置与第一种方法效果一样  -->

   <bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail">
     <ref bean="quartzClock"/>
    </property>
    <property name="startDelay"><!-- 这里是服务启动后延时多少时间,开始计时任务,单位ms -->
     <value>6000</value> 
    
    
    </property>
    <property name="repeatInterval"><!-- 这里是每隔多长时间就进行一次计时任务,单位ms -->
     <value>2000</value> 
      
     
    </property>
   </bean>
  
<!-- 这种配置可以精确几点执行定时任务 -->
<!-- 定义了任务的执行方式 -->
<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean" >
   <property name="jobDetail">
    <ref bean="quartzClock"></ref>
   </property>
   <property name="cronExpression">
    <value>3 * * * * ?</value>
   </property>
</bean>
 
<!--第三步 启动定时任务,注意这里的ref bean -->
<bean  id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
   <property name="triggers">
    <list>
     <ref bean="cronQuartzClock"></ref>
    </list>
   </property>
</bean>

</beans>

//@Component
public class TaskServer  extends QuartzJobBean{

// public TaskServer() {
//  super();
//   System.out.println("每次都创建一个新对象 任务开始");
// }

@Override
protected void executeInternal(JobExecutionContext arg0)
     throws JobExecutionException {
//    System.out.println("执行定时任务..!"+String.format("%1$tF %1$tT" ,new Date()));
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics