+-
初次使用Spring--quartz
   最近由于公司人力资源部门的需要,由于公司每天的日报太多,想做一个定时的自动归档报表的功能,在我领导金导的帮助下,基本是实现了功能。下面是我自己总结的步骤:

   1、 建立一个spring-task.xml(名字随便)的xml文件,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<!-- 要干活的类-->
 <bean id="autoRunArchiveDayService" class="com.ahtec.oa.service.impl.AutoRunArchiveDayServiceImpl" />
 
 <!-- Quartz -->
 <bean id="archiveDayDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="autoRunArchiveDayService" />

    <!-- 类中要干活的方法archiveDay()-->
  <property name="targetMethod" value="archiveDay" />
 </bean>

 <bean id="archiveDayTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <!-- see the example of method invoking job above -->
  <property name="jobDetail" ref="archiveDayDetail" />


<!-- 在服务器启动后5秒中启用方法archiveDay()-->
  <property name="startDelay" value="5000" />

<!-- 每次执行方法archiveDay()的间隔时间为50秒-->
  <property name="repeatInterval" value="50000" />
 </bean>

 <bean id="archiveDayCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="archiveDayDetail" />

   <!-- cronExpression配置,请参看我的上一篇日志-->
  <property name="cronExpression" value="0 0 0 ? * 6" />
 </bean>

 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="archiveDayCronTrigger" />
    <!--
     <ref bean="archiveDayTrigger" />     -->
   </list>
  </property>
 </bean>
</beans>



2、编写AutoRunArchiveDayServiceImpl类(这里省略),类中必须要有archiveDay()  方法。

可以在archiveDay()方法中打印 :System.out.println(i++);


3、在web.xml中增加

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring-task.xml</param-value>
 </context-param>

4、配置好后启动服务器,将会在控制台下面看见 打印 0 1 2 ...