`
javaeyetodj
  • 浏览: 426221 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

OSCACHE集群功能 转

阅读更多

转自 http://benx.iteye.com/blog/606083

Oscache的集群功能没有完全实现,只是实现了flush的接口,需要用户自己去实现它的其它集群接口(不知道他是出于什么考虑). 如果采用Jgroups组件,则需要继承自JavaGroupsBroadcastingListener抽象类,继承它的handleClusterNotification方法,完成集群其它功能的实现:
注意:集群中传递的对象必须实现Serializable接口。

Java代码 复制代码
  1. public class JavaGroupsBroadcastingListenerImpl extends     
  2.         JavaGroupsBroadcastingListener {     
  3.     public void handleClusterNotification(ClusterNotification message) {     
  4.              
  5.         switch (message.getType()) {     
  6.         case CacheConstants.CLUSTER_ENTRY_ADD:     
  7.             System.out.println("集群新增:" + message.getData());     
  8.             if(message.getData() instanceof QflagCacheEvent) {     
  9.                 QflagCacheEvent event = (QflagCacheEvent)message.getData();     
  10.                 cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);     
  11.             }     
  12.             break;     
  13.         case CacheConstants.CLUSTER_ENTRY_UPDATE:     
  14.             System.out.println("集群更新:" + message.getData());     
  15.             if(message.getData() instanceof QflagCacheEvent) {     
  16.                 QflagCacheEvent event = (QflagCacheEvent)message.getData();     
  17.                 cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);     
  18.             }     
  19.             break;     
  20.         case CacheConstants.CLUSTER_ENTRY_DELETE:     
  21.             System.out.println("集群删除:" + message.getData());     
  22.             if(message.getData() instanceof QflagCacheEvent) {     
  23.                 QflagCacheEvent event = (QflagCacheEvent)message.getData();     
  24. //              cache.removeEntry(event.getKey(),event.getOrigin());     
  25.                 cache.removeEntry(event.getKey());     
  26.             }     
  27.             break;     
  28.         }     
  29.      
  30.     }     
  31.          
  32.     public void cacheEntryAdded(CacheEntryEvent event) {     
  33.         super.cacheEntryAdded(event);     
  34.         if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {     
  35.             sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));     
  36.         }     
  37.     }     
  38.      
  39. //  @Override     
  40. //  public void cacheEntryFlushed(CacheEntryEvent event) {     
  41. //           
  42. //      super.cacheEntryFlushed(event);     
  43. //      if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {     
  44. //          sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new UcallCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));     
  45. //      }     
  46. //  }     
  47.      
  48.     @Override     
  49.     public void cacheEntryRemoved(CacheEntryEvent event) {     
  50.              
  51.         super.cacheEntryRemoved(event);     
  52.         if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {     
  53.             sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_DELETE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));     
  54.         }     
  55.     }     
  56.      
  57.     @Override     
  58.     public void cacheEntryUpdated(CacheEntryEvent event) {     
  59.              
  60.         super.cacheEntryUpdated(event);     
  61.         if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {     
  62.             sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_UPDATE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));     
  63.         }     
  64.     }     
  65.          
  66. }    
public class JavaGroupsBroadcastingListenerImpl extends  
        JavaGroupsBroadcastingListener {  
    public void handleClusterNotification(ClusterNotification message) {  
          
        switch (message.getType()) {  
        case CacheConstants.CLUSTER_ENTRY_ADD:  
            System.out.println("集群新增:" + message.getData());  
            if(message.getData() instanceof QflagCacheEvent) {  
                QflagCacheEvent event = (QflagCacheEvent)message.getData();  
                cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);  
            }  
            break;  
        case CacheConstants.CLUSTER_ENTRY_UPDATE:  
            System.out.println("集群更新:" + message.getData());  
            if(message.getData() instanceof QflagCacheEvent) {  
                QflagCacheEvent event = (QflagCacheEvent)message.getData();  
                cache.putInCache(event.getKey(), event.getEntry().getContent(),null,null,CLUSTER_ORIGIN);  
            }  
            break;  
        case CacheConstants.CLUSTER_ENTRY_DELETE:  
            System.out.println("集群删除:" + message.getData());  
            if(message.getData() instanceof QflagCacheEvent) {  
                QflagCacheEvent event = (QflagCacheEvent)message.getData();  
//              cache.removeEntry(event.getKey(),event.getOrigin());  
                cache.removeEntry(event.getKey());  
            }  
            break;  
        }  
  
    }  
      
    public void cacheEntryAdded(CacheEntryEvent event) {  
        super.cacheEntryAdded(event);  
        if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {  
            sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));  
        }  
    }  
  
//  @Override  
//  public void cacheEntryFlushed(CacheEntryEvent event) {  
//        
//      super.cacheEntryFlushed(event);  
//      if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {  
//          sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_ADD, new UcallCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));  
//      }  
//  }  
  
    @Override  
    public void cacheEntryRemoved(CacheEntryEvent event) {  
          
        super.cacheEntryRemoved(event);  
        if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {  
            sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_DELETE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));  
        }  
    }  
  
    @Override  
    public void cacheEntryUpdated(CacheEntryEvent event) {  
          
        super.cacheEntryUpdated(event);  
        if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {  
            sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_UPDATE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));  
        }  
    }  
      
}  

 

Java代码 复制代码
  1. package qflag.ucall.cache;     
  2. public class CacheConstants {     
  3.     /**   
  4.      * 添加缓存对象操作   
  5.      */     
  6.     public final static int ACTION_ADD_OBJ = 1;     
  7.     /**   
  8.      * 更新缓存对象操作   
  9.      */     
  10.     public final static int ACTION_UPDATE_OBJ = 2;     
  11.     /**   
  12.      * 删除缓存对象操作   
  13.      */     
  14.     public final static int ACTION_DELETE_OBJ = 3;     
  15.     /**   
  16.      * 刷新缓存对象   
  17.      */     
  18.     public final static int ACTION_FLUSH_OBJ = 4;     
  19.          
  20.          
  21.     /**   
  22.      * 集群entry add处理   
  23.      */     
  24.     public final static int CLUSTER_ENTRY_ADD = 20;     
  25.          
  26.     /**   
  27.      * 集群entry update处理   
  28.      */     
  29.     public final static int CLUSTER_ENTRY_UPDATE = 21;     
  30.          
  31.     /**   
  32.      * 集群entry delete处理   
  33.      */     
  34.     public final static int CLUSTER_ENTRY_DELETE = 22;     
  35. }    
package qflag.ucall.cache;  
public class CacheConstants {  
    /** 
     * 添加缓存对象操作 
     */  
    public final static int ACTION_ADD_OBJ = 1;  
    /** 
     * 更新缓存对象操作 
     */  
    public final static int ACTION_UPDATE_OBJ = 2;  
    /** 
     * 删除缓存对象操作 
     */  
    public final static int ACTION_DELETE_OBJ = 3;  
    /** 
     * 刷新缓存对象 
     */  
    public final static int ACTION_FLUSH_OBJ = 4;  
      
      
    /** 
     * 集群entry add处理 
     */  
    public final static int CLUSTER_ENTRY_ADD = 20;  
      
    /** 
     * 集群entry update处理 
     */  
    public final static int CLUSTER_ENTRY_UPDATE = 21;  
      
    /** 
     * 集群entry delete处理 
     */  
    public final static int CLUSTER_ENTRY_DELETE = 22;  
}  

 

Java代码 复制代码
  1.   
  2. package qflag.ucall.cache.event;     
  3.      
  4. import java.io.Serializable;     
  5.      
  6. import com.opensymphony.oscache.base.Cache;     
  7. import com.opensymphony.oscache.base.CacheEntry;     
  8. import com.opensymphony.oscache.base.events.CacheEntryEvent;     
  9. import com.opensymphony.oscache.base.events.CacheEvent;     
  10.      
  11. public class QflagCacheEvent extends CacheEvent implements Serializable {     
  12.     /**   
  13.      * The cache where the entry resides.   
  14.      */     
  15.     private Cache map = null;     
  16.      
  17.     /**   
  18.      * The entry that the event applies to.   
  19.      */     
  20.     private CacheEntry entry = null;     
  21.      
  22.     /**   
  23.      * Constructs a cache entry event object with no specified origin   
  24.      *    
  25.      * @param map   
  26.      *            The cache map of the cache entry   
  27.      * @param entry   
  28.      *            The cache entry that the event applies to   
  29.      */     
  30.     public QflagCacheEvent(Cache map, CacheEntry entry) {     
  31.         this(map, entry, null);     
  32.     }     
  33.      
  34.     /**   
  35.      * Constructs a cache entry event object   
  36.      *    
  37.      * @param map   
  38.      *            The cache map of the cache entry   
  39.      * @param entry   
  40.      *            The cache entry that the event applies to   
  41.      * @param origin   
  42.      *            The origin of this event   
  43.      */     
  44.     public QflagCacheEvent(Cache map, CacheEntry entry, String origin) {     
  45.         super(origin);     
  46.         this.map = map;     
  47.         this.entry = entry;     
  48.     }     
  49.      
  50.     /**   
  51.      * Retrieve the cache entry that the event applies to.   
  52.      */     
  53.     public CacheEntry getEntry() {     
  54.         return entry;     
  55.     }     
  56.      
  57.     /**   
  58.      * Retrieve the cache entry key   
  59.      */     
  60.     public String getKey() {     
  61.         return entry.getKey();     
  62.     }     
  63.      
  64.     /**   
  65.      * Retrieve the cache map where the entry resides.   
  66.      */     
  67.     public Cache getMap() {     
  68.         return map;     
  69.     }     
  70.      
  71.     public String toString() {     
  72.         return "key=" + entry.getKey();     
  73.     }     
  74. }    
package qflag.ucall.cache.event;  
  
import java.io.Serializable;  
  
import com.opensymphony.oscache.base.Cache;  
import com.opensymphony.oscache.base.CacheEntry;  
import com.opensymphony.oscache.base.events.CacheEntryEvent;  
import com.opensymphony.oscache.base.events.CacheEvent;  
  
public class QflagCacheEvent extends CacheEvent implements Serializable {  
    /** 
     * The cache where the entry resides. 
     */  
    private Cache map = null;  
  
    /** 
     * The entry that the event applies to. 
     */  
    private CacheEntry entry = null;  
  
    /** 
     * Constructs a cache entry event object with no specified origin 
     *  
     * @param map 
     *            The cache map of the cache entry 
     * @param entry 
     *            The cache entry that the event applies to 
     */  
    public QflagCacheEvent(Cache map, CacheEntry entry) {  
        this(map, entry, null);  
    }  
  
    /** 
     * Constructs a cache entry event object 
     *  
     * @param map 
     *            The cache map of the cache entry 
     * @param entry 
     *            The cache entry that the event applies to 
     * @param origin 
     *            The origin of this event 
     */  
    public QflagCacheEvent(Cache map, CacheEntry entry, String origin) {  
        super(origin);  
        this.map = map;  
        this.entry = entry;  
    }  
  
    /** 
     * Retrieve the cache entry that the event applies to. 
     */  
    public CacheEntry getEntry() {  
        return entry;  
    }  
  
    /** 
     * Retrieve the cache entry key 
     */  
    public String getKey() {  
        return entry.getKey();  
    }  
  
    /** 
     * Retrieve the cache map where the entry resides. 
     */  
    public Cache getMap() {  
        return map;  
    }  
  
    public String toString() {  
        return "key=" + entry.getKey();  
    }  
}  

 

分享到:
评论

相关推荐

    oscache 集群和数据同步

    NULL 博文链接:https://zhenghuazhi.iteye.com/blog/1135620

    OSCache配置说明文档

    OSCache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。OSCache是一个广泛采用的高性能的J2EE缓存框架,OSCache能用于任何Java应用程序的普通的缓存解决...

    oscache文档

    OSCache标记库由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。OSCache是个一个广泛采用的高性能的J2EE缓存框架,OSCache能用于任何Java应用程序的普通的...

    oscache的例子

    OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存...

    oscache-2.1.jar

    oscache-2.1.jar oscache-2.1.jar

    oscache(JSP定制标记应用)

    javaweb做页面缓存常用,OSCache是一个工业级的J2EE缓存实现。OSCache不但能缓存java对象,还可以缓存页面,http请求和二进制内容,...通过应用OSCache,我们不但可以实现通常的Cache功能,还能够改善系统的稳定性。

    Oscache使用手册

    OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存...

    oscache缓存技术入门实例

    oscache缓存技术入门实例

    用OSCache进行缓存对象

    1、OSCache是什么? 2、OSCache的特点 3、有关“用OSCache进行缓存对象”的研究

    OSCache学习例子 实例

    OSCache学习例子 实例 很好的与j2ee结合

    Oscache框架的搭建步骤

    使用oscache进行缓存,大大提高web系统运行效率

    oscache的demo

    oscache的简单功能实现的,可以下载下来看看

    oscache 使用介紹

    OSCache是OpenSymphony这个开源项目众多Projects中的一个。他是一个高效的J2EE缓存框架,能够很好的解决动态网站速度的问题。

    OSCache需要的包

    oscache.tld,oscahe.properties,oscache-2.1.jar,commons

    oscache-java缓存框架

    oscache-java缓存框架插件和安装教程,使用教程一步到位

    oscache jar包下载

    oscache-2.4.1.jar资源包,用于java缓存、jsp页面缓存

    oscache-2.4.1-full

    OSCache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。 该版本为2.4.1

    oscache说明

    oscache的简单介绍

    oscache的使用

    oscache的使用

    oscache详细配置文档

    本文叙述了如何使用oscanche,最后的配置需要在oscache.properties中完成

Global site tag (gtag.js) - Google Analytics