ClusterConsumer.java :  » Cache » shiftone-cache » org » shiftone » cache » decorator » cluster » Java Open Source

Java Open Source » Cache » shiftone cache 
shiftone cache » org » shiftone » cache » decorator » cluster » ClusterConsumer.java
package org.shiftone.cache.decorator.cluster;



import org.jgroups.Address;
import org.jgroups.blocks.NotificationBus;
import org.shiftone.cache.util.Log;

import java.io.Serializable;


/**
 * @version $Revision: 1.2 $
 * @author $Author: jeffdrost $
 */
public class ClusterConsumer implements NotificationBus.Consumer
{

    private static final Log          LOG = new Log(ClusterConsumer.class);
    private final ClusterBus          bus;
    private final ClusterCacheFactory clusterCacheFactory;
    private int                       members = 0;

    public ClusterConsumer(ClusterBus bus)
    {
        this.bus                 = bus;
        this.clusterCacheFactory = bus.getClusterCacheFactory();
    }


    public void handleNotification(Serializable serializable)
    {

        if (serializable instanceof Notification)
        {
            clusterCacheFactory.handleNotification((Notification) serializable);
        }
    }


    public Serializable getCache()
    {
        return null;
    }


    public synchronized void memberJoined(Address address)
    {

        LOG.info("memberJoined " + address);

        members++;

        updateAlone();
    }


    public synchronized void memberLeft(Address address)
    {

        LOG.info("memberJoined " + address);

        members--;

        updateAlone();
    }


    private void updateAlone()
    {
        bus.setAloneInCluster(members == 1);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.