<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    空間站

    北極心空

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    應項目需要做了一個定時更新的 cache 框架,采用 spring+quartz 很方便的實現,可以適用任何需要定時才更新的地方,比如靜態網頁 cache 等。代碼很簡單:

    ---------------------------------QuartzCacheHandler-------------------------------------

    package  com.bankcomm.cache;

     

    import  org.apache.commons.logging.Log;

    import  org.apache.commons.logging.LogFactory;

    import  org.springframework.context.ApplicationContext;

    import  org.springframework.context.support.ClassPathXmlApplicationContext;

     

    import  com.bankcomm.util.ContextUtil;

     

    public   class  QuartzCacheHandler  {

           
    private   static  ApplicationContext actx;

     

           Log log 
    =  LogFactory.getLog( this .getClass());

     

           
    /**

            * 程序載入配置文件<br>

            * Author:pesome<br>

            * Time:2006-12-8 上午10:29:26<br>

            
    */


           
    public   static   void  init()  {

                  
    try   {

                         actx 
    =   new  ClassPathXmlApplicationContext(

                                       
    new  String[]  " quartzCache*.xml "  } );

                  }
      catch  (Exception e)  {

                         e.printStackTrace();

                         
    throw   new  RuntimeException();

                  }


           }


     

           
    private  QuartzCacheHandler()  {

           }


     

           
    /**

            * 在程序載入配置文件時使用<br>

            * Author:pesome<br>

            * Time:2006-12-8 上午10:28:07<br>

            * 

            * 
    @param  beanName

            * 
    @param  key

            * 
    @return

            
    */


           
    public   static  Object getSe(String beanName, String key)  {

                  
    return  ((QuartzCache) actx.getBean(beanName)).get(key);

           }


     

           
    /**

            * 在web容器中,初始化時載入配置文件時使用<br>

    * Author:pesome<br>

            * Time:2006-12-8 上午10:28:40<br>

            * 

            * 
    @param  beanName

            * 
    @param  key

            * 
    @return

            
    */


           
    public   static  Object get(String beanName, String key)  {

                  
    return  ((QuartzCache) ContextUtil.getBean(beanName)).get(key);

           }


    }


     

    -----------------------------------QuartzCache-----------------------------------------------

    package  com.bankcomm.cache;

     

    import  java.util.HashMap;

    import  java.util.Map;

     

    import  org.apache.commons.logging.Log;

    import  org.apache.commons.logging.LogFactory;

     

    public   abstract   class  QuartzCache  {

           
    private  Log log  =  LogFactory.getLog( this .getClass());

     

           
    protected  Map cacheMap  =   new  HashMap();

     

           
    /**

            * 抽象方法由具體的cache類實現,一般為調用put方法<br>

            * Author:pesome<br>

            * Time:2006-12-7 下午05:47:26<br>

            
    */


           
    public   abstract   void  refresh();

     

           
    public  Object get(String key)  {

                  
    return  cacheMap.get(key);

           }


     

           
    public   void  put(String key, Object value)  {

                  cacheMap.put(key, value);

                  log.info(
    " put to quartz cache key= "   +  key  +   " ,value= "   +  value);

           }


    }


    ---------------------------------------------------------------------------------------------------------

    Web.xml 中只需加 2 句:

     

    < context-param >
            
    < param-name > contextConfigLocation </ param-name >
            
    < param-value > /WEB-INF/applicationContext*.xml </ param-value >
        
    </ context-param >

        
    < listener >
            
    < listener-class >
                com.bankcomm.util.MyContextLoaderListener
            
    </ listener-class >
        
    </ listener >

     

    最后是applicationContext_quartzCache.xml配置文件,就是標準的spring結合quartz的配置文件:

    <? xml version="1.0" encoding="GB2312" ?>

    <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

                           "http://www.springframework.org/dtd/spring-beans.dtd"
    >

    < beans >

         
    < bean

             
    class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >

             
    < property  name ="triggers" >

                  
    < list >

                       
    < ref  local ="simpleTrigger"   />

                  
    </ list >

             
    </ property >

         
    </ bean >

     

         
    < bean  id ="methodInvokingJobDetail"

             class
    ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >

             
    < property  name ="targetObject" >

                  
    < ref  bean ="simpleCache"   />

             
    </ property >

             
    < property  name ="targetMethod" >

                  
    < value > refresh </ value >

             
    </ property >

         
    </ bean >

     

         
    < bean  id ="simpleTrigger"

             class
    ="org.springframework.scheduling.quartz.SimpleTriggerBean" >

             
    < property  name ="jobDetail" >

                  
    <!--  see the example of method invoking job above  -->

                  
    < ref  bean ="methodInvokingJobDetail"   />

             
    </ property >

             
    < property  name ="startDelay" >

                  
    < value > 0 </ value >

             
    </ property >

             
    <!--  set the refresh interval,millisecond  -->

             
    < property  name ="repeatInterval" >

                  
    < value > 2000 </ value >

             
    </ property >

         
    </ bean >

         

         
    <!--  custom job beans  -->

        
    < bean  id ="simpleCache"  class ="com.bankcomm.cache.SimpleCache" ></ bean >

    </ beans >

     

    寫自己的QuartzCache子類并實現refresh方法,然后在配置文件中定義bean和相應的trigger就能方便的實現定時cache了。示例中使用了 SimpleTriggerBean ,每2s更新一次。也可以使用CronTriggerBean,每天定時更新。 使用 cache ,只需調用 QuartzCacheHandler get getSe 就行, get 是在由 web 容器啟動 quartz 的場合使用, getSe 在使用 init 方法啟動時使用。 Get 中調用了自己寫的一個 ContextUtil ,它包含一個靜態的 applicationContex 的引用,在 spring 容器啟動后由 MyContextLoaderListener (重載 spring ContextLoaderListener )填充。

    這些東西也就幾個小時搞定,多虧了springquartz這些開源軟件啊。要自己用timer實現,費時費力,擴展性,易用性等也會差很多。

    posted on 2007-04-19 17:44 蘆葦 閱讀(1303) 評論(1)  編輯  收藏 所屬分類: Spring

    Feedback

    # re: 一個定時更新cache框架 2014-04-28 21:10 zuidaima
    請參考代碼:spring集成quartz實現的定時更新cache的代碼配置,下載地址:http://www.zuidaima.com/share/1799824505031680.htm  回復  更多評論
      

    主站蜘蛛池模板: 国产一精品一AV一免费| 人成免费在线视频| 外国成人网在线观看免费视频| 亚洲 小说区 图片区 都市| 亚洲中文无码卡通动漫野外| 免费做爰猛烈吃奶摸视频在线观看 | 亚洲人成色7777在线观看| 黄 色一级 成 人网站免费| 伊人婷婷综合缴情亚洲五月| 国产在线观看免费av站| 亚洲国产精品一区二区成人片国内| a级毛片在线视频免费观看| 亚洲嫩模在线观看| 亚洲精品免费网站| 亚洲欧美日韩中文二区| 免费在线视频一区| a级毛片免费全部播放无码| 18gay台湾男同亚洲男同| 在线v片免费观看视频| 亚洲乱码av中文一区二区| 亚洲?V无码成人精品区日韩| caoporn国产精品免费| 久久亚洲AV午夜福利精品一区| 亚洲一区二区三区免费观看| 亚洲成AV人片在WWW| 国产精品亚洲二区在线观看| 日韩精品免费视频| 中文字幕精品三区无码亚洲| vvvv99日韩精品亚洲| 久久精品一区二区免费看| 亚洲午夜在线播放| 久久久久亚洲AV成人网人人网站 | 伊人久久亚洲综合影院| 国产免费无码AV片在线观看不卡| 亚洲激情视频网站| 免费人成年轻人电影| 99免费在线观看视频| 国产成人高清亚洲一区久久 | 亚洲精品无码精品mV在线观看| 69式互添免费视频| 一个人免费观看日本www视频|