夢幻之旅

          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 閱讀(1275) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 梁平县| 荣昌县| 长宁县| 牙克石市| 全州县| 黔东| 永平县| 县级市| 柘城县| 措勤县| 溧阳市| 潮州市| 合水县| 中宁县| 页游| 灵丘县| 德令哈市| 元氏县| 宁安市| 桑日县| 恩平市| 金乡县| 漳平市| 香河县| 舒兰市| 门源| 和平县| 信宜市| 鄢陵县| 盐城市| 阜新| 南木林县| 仙居县| 固镇县| 剑川县| 磐石市| 六安市| 藁城市| 吉安县| 钦州市| 弥渡县|