spring集成quartz实现的定时更新cache的代码配置

原文:spring集成quartz实现的定时更新cache的代码配置

源代码下载地址:http://www.zuidaima.com/share/1799824505031680.htm

应项目需要做了一个定时更新的 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 )填充。

原文地址:http://www.blogjava.net/i369/articles/112007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值