spring整合quartz

jopen 10年前發布 | 20K 次閱讀 Quartz 作業調度框架

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來做更簡單,更好,更靈活.

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!