梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
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>
posted on 2008-08-19 22:59 HUIKK 阅读(1252) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: