replicatedhashtable.java
来自「JGRoups源码」· Java 代码 · 共 511 行 · 第 1/2 页
JAVA
511 行
// $Id: ReplicatedHashtable.java,v 1.14 2006/03/27 08:34:24 belaban Exp $package org.jgroups.blocks;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.jgroups.*;import org.jgroups.util.Util;import java.io.Serializable;import java.util.*;/** * Provides the abstraction of a java.util.Hashtable that is replicated at several * locations. Any change to the hashtable (clear, put, remove etc) will transparently be * propagated to all replicas in the group. All read-only methods will always access the * local replica.<p> * Both keys and values added to the hashtable <em>must be serializable</em>, the reason * being that they will be sent across the network to all replicas of the group. Having said * this, it is now for example possible to add RMI remote objects to the hashtable as they * are derived from <code>java.rmi.server.RemoteObject</code> which in turn is serializable. * This allows to lookup shared distributed objects by their name and invoke methods on them, * regardless of one's onw location. A <code>ReplicatedHashtable</code> thus allows to * implement a distributed naming service in just a couple of lines.<p> * An instance of this class will contact an existing member of the group to fetch its * initial state.<p> * Contrary to DistributedHashtable, this class does not make use of RpcDispatcher (and RequestCorrelator) * but uses plain asynchronous messages instead. * @author Bela Ban * @author <a href="mailto:aolias@yahoo.com">Alfonso Olias-Sanz</a> * @todo implement putAll() [similar to DistributedHashtable]. */public class ReplicatedHashtable extends Hashtable implements MessageListener, MembershipListener { public interface Notification { void entrySet(Object key, Object value); void entryRemoved(Object key); void viewChange(Vector new_mbrs, Vector old_mbrs); void contentsSet(Map new_entries); } public interface StateTransferListener { void stateTransferStarted(); void stateTransferCompleted(boolean success); } transient Channel channel; transient PullPushAdapter adapter=null; final transient Vector notifs=new Vector(); // to be notified when mbrship changes final transient Vector members=new Vector(); // keeps track of all DHTs final transient List state_transfer_listeners=new ArrayList(); transient boolean state_transfer_running=false; /** Determines when the updates have to be sent across the network, avoids sending unnecessary * messages when there are no member in the group */ private transient boolean send_message=false; protected final transient Log log=LogFactory.getLog(this.getClass()); /** * Creates a ReplicatedHashtable * @param groupname The name of the group to join * @param factory The ChannelFactory which will be used to create a channel * @param properties The property string to be used to define the channel * @param state_timeout The time to wait until state is retrieved in milliseconds. A value of 0 means wait forever. */ public ReplicatedHashtable(String groupname, ChannelFactory factory, StateTransferListener l, String properties, long state_timeout) { if(l != null) addStateTransferListener(l); try { channel=factory != null ? factory.createChannel(properties) : new JChannel(properties); channel.connect(groupname); adapter=new PullPushAdapter(channel, this, this); adapter.setListener(this); getInitState(channel, state_timeout); } catch(Exception e) { if(log.isErrorEnabled()) log.error("exception=" + e); } } private void getInitState(Channel channel, long state_timeout) throws ChannelClosedException, ChannelNotConnectedException { try { notifyStateTransferStarted(); boolean rc=channel.getState(null, state_timeout); if(rc) { if(log.isInfoEnabled()) log.info("state was retrieved successfully"); } else { if(log.isInfoEnabled()) log.info("state could not be retrieved (first member)"); notifyStateTransferCompleted(false); } } catch(ChannelClosedException ex) { notifyStateTransferCompleted(false); throw ex; } catch(ChannelNotConnectedException ex2) { notifyStateTransferCompleted(false); throw ex2; } } public ReplicatedHashtable(String groupname, ChannelFactory factory, String properties, long state_timeout) { this(groupname, factory, null, properties, state_timeout); } public ReplicatedHashtable(JChannel channel, long state_timeout) throws ChannelClosedException, ChannelNotConnectedException { this(channel, null, state_timeout); } public ReplicatedHashtable(JChannel channel, StateTransferListener l, long state_timeout) throws ChannelClosedException, ChannelNotConnectedException { this.channel=channel; this.adapter=new PullPushAdapter(channel, this, this); this.adapter.setListener(this); if(l != null) addStateTransferListener(l); getInitState(channel, state_timeout);// boolean rc=channel.getState(null, state_timeout);// if(rc)// if(log.isInfoEnabled()) log.info("state was retrieved successfully");// else// if(log.isInfoEnabled()) log.info("state could not be retrieved (first member)"); } public boolean stateTransferRunning() { return state_transfer_running; } public Address getLocalAddress() { return channel != null ? channel.getLocalAddress() : null; } public Channel getChannel() { return channel; } public void addNotifier(Notification n) { if(!notifs.contains(n)) notifs.addElement(n); } public final void addStateTransferListener(StateTransferListener l) { if(l != null && !(state_transfer_listeners.contains(l))) state_transfer_listeners.add(l); } public void removeStateTransferListener(StateTransferListener l) { if(l != null) state_transfer_listeners.remove(l); } /** * Maps the specified key to the specified value in the hashtable. Neither of both parameters can be null * @param key - the hashtable key * @param value - the value * @return the previous value of the specified key in this hashtable, or null if it did not have one */ public Object put(Object key, Object value) { Message msg; Object prev_val=null; prev_val=get(key); //Changes done by <aos> //if true, send message to the group if(send_message == true) { try { msg=new Message(null, null, new Request(Request.PUT, key, value)); channel.send(msg); //return prev_val; } catch(Exception e) { //return null; } } else { super.put(key, value); //don't have to do prev_val = super.put(..) as is done at the beginning } return prev_val; } /** * Copies all of the mappings from the specified Map to this Hashtable These mappings will replace any mappings that this Hashtable had for any of the keys currently in the specified Map. * @param m - Mappings to be stored in this map */ public void putAll(Map m) { Message msg; //Changes done by <aos> //if true, send message to the group if(send_message == true) { try { msg=new Message(null, null, new Request(Request.PUT_ALL, null, m)); channel.send(msg); } catch(Exception e) { if(log.isErrorEnabled()) log.error("exception=" + e); } } else { super.putAll(m); } } /** * Clears this hashtable so that it contains no keys */ public void clear() { Message msg; //Changes done by <aos> //if true, send message to the group if(send_message == true) { try { msg=new Message(null, null, new Request(Request.CLEAR, null, null)); channel.send(msg); } catch(Exception e) { if(log.isErrorEnabled()) log.error("exception=" + e); } } else { super.clear(); } } /** * Removes the key (and its corresponding value) from the Hashtable. * @param key - the key to be removed. * @return the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping. */ public Object remove(Object key) { Message msg; Object retval=null; retval=get(key); //Changes done by <aos> //if true, propagate action to the group if(send_message == true) { try { msg=new Message(null, null, new Request(Request.REMOVE, key, null)); channel.send(msg); //return retval; } catch(Exception e) { //return null; } } else { super.remove(key); //don't have to do retval = super.remove(..) as is done at the beginning }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?