ITeye博客

spring 里配置 quartz

hwpok · 2008-08-19 22:59 · Spring · Markdown

package hvp.spring.quartz.bean;

import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

public class MyJob implements Job
{
    
    @SuppressWarnings("unchecked")
    public void execute(JobExecutionContext jec) throws JobExecutionException
    {
        Map dataMap = jec.getJobDetail().getJobDataMap();
        String size = (String) dataMap.get("size");
        // 得到Spring的ApplicationContext,可以访问Spring里的任何Bean了.
        ApplicationContext ac = (ApplicationContext) dataMap
                .get("applicationContext");
        System.out.println(size + ":工作里" + ac.toString());
    }
}

package hvp.spring.quartz.bean;

public class MyService
{
    public void doJob()
    {
        System.out.println("in MyService.doJob().");
    }
}

package hvp.spring.quartz.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestQuartx
{   
    public static void main(String[] args){
        String configPath = "hvp/spring/quartz/bean/quartzBean.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
        ctx.getBean("scheduler");
    }
}

<?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-2.0.xsd">
    <bean name="jobDetail"
        class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass"
            value="hvp.spring.quartz.bean.MyJob" />
        <property name="jobDataAsMap">
            <map>
                <entry key="size" value="10" />
            </map>
        </property>
        <property name="applicationContextJobDataKey"
            value="applicationContext" />
    </bean>
    <bean id="jobDetail_1"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myService" />
        <property name="targetMethod" value="doJob" />
        <property name="concurrent" value="false" />
    </bean>
    <bean id="myService" class="hvp.spring.quartz.bean.MyService" />
    <bean id="simpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetail" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="1000" />
        <property name="repeatCount" value="1000" />
        <property name="jobDataAsMap">
            <map>
                <entry key="count" value="10" />
            </map>
        </property>
    </bean>
    <bean id="checkImagesTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetail_1" />
        <property name="cronExpression" value="0/5 * * * * ?" />
    </bean>
    <bean id="scheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="checkImagesTrigger" />
                <ref bean="simpleTrigger" />
            </list>
        </property>
        <property name="schedulerContextAsMap">
            <map>
                <entry key="timeout" value="30" />
            </map>
        </property>
        <property name="quartzProperties">
            <props>
                <prop key="org.quartz.threadPool.class">
                    org.quartz.simpl.SimpleThreadPool
                </prop>
                <prop key="org.quartz.threadPool.threadCount">10</prop>
            </props>
        </property>
    </bean>
</beans>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。