2008-07-11

Quartz调度任务

package example.chapter9;

import java.util.Date;

public class CheckDiskFreeSpace {

    public void check() {
        // get disk free space:
        long freeSpace = Math.random() > 0.5 ? 100000000 : 200000000;
        System.out.println("Check disk free space at " + new Date());
        if(freeSpace<100*1024*1024) { // <100MB
            System.out.println("Warning! Low disk free space...");
        }
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
>

    <bean name="checkDiskFreeSpace" class="example.chapter9.CheckDiskFreeSpace" />

    <bean name="checkDiskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="checkDiskFreeSpace" />
        <property name="targetMethod" value="check" />
        <property name="concurrent" value="false" />
    </bean>

    <!-- Trigger -->

    <!-- 周期性运行checkDiskJob -->
    <bean id="repeatTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="checkDiskJob" />
        <!-- 1分钟后启动 -->
        <property name="startDelay" value="600" />
        <!-- 5分钟检查一次 -->
        <property name="repeatInterval" value="300" />
    </bean>



    <!-- 启动调度器 -->

    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="repeatTrigger" />
            </list>
        </property>
    </bean>

</beans>



package example.chapter9;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("config.xml");
    }

}
评论
发表评论

您还没有登录,请登录后发表评论

wangyu
搜索本博客
我的相册
4a4a4c43-84f9-3c9d-9259-f29f3081cdc9-thumb
strutsWorkflow
共 3 张
最近加入圈子
存档
最新评论