实现OSCache

我们要集群式同步数据,必须实现这三个方法:

    public void cacheEntryAdded(CacheEntryEvent event) {  
        }  
      
        public void cacheEntryRemoved(CacheEntryEvent event) {  
        }  
      
        public void cacheEntryUpdated(CacheEntryEvent event) {  
        }  


ackage com.test;  
import com.opensymphony.oscache.base.events.CacheEntryEvent;  
import com.opensymphony.oscache.plugins.clustersupport.ClusterNotification;  
import com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener;  
  
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.flushEntry(event.getKey());  
                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);  
        System.out.println("属性添加");  
        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) {  
        System.out.println("属性移除");  
        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) {  
        System.out.println("属性更新");  
        super.cacheEntryUpdated(event);  
        if(!CLUSTER_ORIGIN.equals(event.getOrigin())) {  
            sendNotification(new ClusterNotification(CacheConstants.CLUSTER_ENTRY_UPDATE, new QflagCacheEvent(event.getMap(),event.getEntry(),CLUSTER_ORIGIN)));  
        }  
    }  
      
}  





然后修改配置文件,映射cache.event.listeners类的路径:
    cache.event.listeners=com.test.JavaGroupsBroadcastingListenerImpl  
    cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\  
    mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\  
    PING(timeout=2000;num_initial_members=3):\  
    MERGE2(min_interval=5000;max_interval=10000):\  
    FD_SOCK:VERIFY_SUSPECT(timeout=1500):\  
    pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\  
    UNICAST(timeout=300,600,1200,2400):\  
    pbcast.STABLE(desired_avg_gossip=20000):\  
    FRAG(frag_size=8096;down_thread=false;up_thread=false):\  
    pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)  
    cache.cluster.multicast.ip=231.12.21.132  

你可能感兴趣的:(OScache)