夢幻之旅

          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 閱讀(1271) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 庆城县| 肇州县| 五原县| 隆林| 赞皇县| 南召县| 邢台县| 浮梁县| 东光县| 靖安县| 香格里拉县| 长宁区| 平武县| 波密县| 萨迦县| 定边县| 贵南县| 神农架林区| 开远市| 常德市| 峡江县| 南京市| 砚山县| 荣成市| 兴城市| 乌苏市| 屯昌县| 新绛县| 慈利县| 桦南县| 重庆市| 凤城市| 泸州市| 合阳县| 改则县| 平和县| 巴中市| 曲水县| 德令哈市| 武邑县| 吴川市|