spring與自動調度任務(一)

          最近把以前設想做搜索引擎的想法再次提出來。剛入門4天吧。
          在想到建立索引的時候要用自動調度的方法。
          原因很簡單,你的索引要更新的。所以想到了自動調度。當然那就想到了spring。
          下面是自己自動調度的一些學習。
          這里只采用jdk自帶的timer進行的,準備在下篇文章中用Quartz調度器。
          首先建立你自己要運行的類。

          package com.duduli.li;

          public class Display {

              
          public void disp(){
                  System.out.println(
          "自動控制測試");
              }
          }
          一個簡單的java bean,其中在這里你可以替換自己的任務。
          然后就是編寫調度程序,這里要繼承jdk中的TimerTask類,復寫他的run方法。
          package com.duduli.li;

          import java.util.TimerTask;

          public class AutoRan extends TimerTask {
              
          //set方法是spring的DI
              private Display display;
              
              
          public void setDisplay(Display display) {
                  
          this.display = display;
              }
              @Override
              
          public void run() {
                  display.disp();
              }
          }
          然后就是重要的一步,編寫applicationsContext.xml了。
          <?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.5.xsd">
              
              
          <bean id="display"
                  class
          ="com.duduli.li.Display">
              
          </bean>
              
          <bean id="atuoRun"
                  class
          ="com.duduli.li.AutoRan">
                  
          <property name="display" ref="display"></property>
              
          </bean>
              
              
          <bean id="aR"
              class
          ="org.springframework.scheduling.timer.ScheduledTimerTask">
                  
          <property name="timerTask" ref="atuoRun"></property>
          <!--
          period多長時間運行一次,delay表示允許你當任務第一次運行前應該等待多久
          -->
                  
          <property name="period" value="5000"></property>
                  
          <property name="delay" value="2000"></property>    
              
          </bean>
              
              
          <bean id="test"
              class
          ="org.springframework.scheduling.timer.TimerFactoryBean">
                  
          <property name="scheduledTimerTasks">
                      
          <list>
          <!--
          這里使用list,可以調度多個bean,
          -->
                          
          <ref bean="aR"/>
                      
          </list>
                  
          </property>
              
          </bean>
          </beans>

          再來就是客戶端調度了。
          package com.duduli.li;

          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class Client {

              
          public static void main(String[] args) {
                  BeanFactory factory 
          = new ClassPathXmlApplicationContext("applicationContext.xml");
                  factory.getBean(
          "test");
              }
          }

          posted on 2009-03-10 14:19 duduli 閱讀(1170) 評論(1)  編輯  收藏 所屬分類: java

          評論

          # re: spring與自動調度任務 2009-03-10 16:56 隔葉黃鶯

          如果為了 Timer 而用 Spring,那就是大材了。  回復  更多評論   

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地攤兒

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          @duduli
          主站蜘蛛池模板: 于都县| 吉安市| 宝兴县| 黄浦区| 济宁市| 慈利县| 原平市| 嘉黎县| 松江区| 锦屏县| 二连浩特市| 玛沁县| 鞍山市| 灵璧县| 河南省| 尼木县| 新竹市| 珲春市| 开原市| 响水县| 郎溪县| 垣曲县| 宿迁市| 珲春市| 阿瓦提县| 出国| 措勤县| 尉氏县| 二连浩特市| 五寨县| 剑河县| 原阳县| 安达市| 阿勒泰市| 华坪县| 阜新市| 紫金县| 平谷区| 景德镇市| 雅江县| 平乡县|