spring整合quartz

jopen 10年前

spring3.0整合quartz1.6的配置

applicationContext-quartz.xml是整合的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:aop="http://www.springframework.org/schema/aop"   xmlns:tx="http://www.springframework.org/schema/tx"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"   default-autowire="byName" default-lazy-init="false">     <bean id="back_class_job"    class="com.yh.logics.service.QuartzManager">   </bean>   <!--  定义目标bean和bean中的方法  -->   <bean id="Job1"    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <property name="targetObject">     <ref local="back_class_job" />    </property>    <!--  要执行的方法名称  -->    <property name="targetMethod">     <value>back_this_mysql_</value>    </property>   </bean>   <!--  定义目标bean和bean中的方法  -->   <bean id="Job2"    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <property name="targetObject">     <ref local="back_class_job" />    </property>    <!--  要执行的方法名称  -->    <property name="targetMethod">     <value>back_this_app_</value>    </property>   </bean>      <!--定义触发的时间-->   <bean id="cron1"    class="org.springframework.scheduling.quartz.CronTriggerBean">    <property name="jobDetail">     <ref bean="Job1" />    </property>    <!-- 每天的,每隔4小时的1分1秒,去看备份情况,如果没有备份就备份否则跳过 -->    <property name="cronExpression">     <value>1 1 */4 * * ?</value>    </property>   </bean>      <!--定义触发的时间-->   <bean id="cron2"    class="org.springframework.scheduling.quartz.CronTriggerBean">    <property name="jobDetail">     <ref bean="Job2" />    </property>    <!-- 每3天的,每隔10小时的1分1秒,去看备份情况,如果没有备份就备份否则跳过 -->    <property name="cronExpression">     <value>1 1 */10 */3 * ?</value>    </property>   </bean>         <!--  管理触发器  -->   <bean autowire="no"    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">     <list>      <ref local="cron1" />      <ref local="cron2" />     </list>    </property>   </bean>      </beans>

涉及到的2个java方法(com.yh.logics.service.QuartzManager):

/**    * 定时任务,备份数据库    * @throws InterruptedException    * @throws IOException    */   public int back_this_mysql_() throws IOException, InterruptedException {    User sysatem = new User();    sysatem.setUserno("system");    return back_this_mysql_dump_ByUser(sysatem);   }      /**    * 定时任务,备份整个程序    */   public int back_this_app_() {    User sysatem = new User();    sysatem.setUserno("system");    return back_this_app_ByUser(sysatem);   }

这些个定时任务用主机上的crontab和shell来做更简单,更好,更灵活.