org.jgroups.blocks
Class DistributedHashtable

java.lang.Object
  extended byjava.util.Dictionary
      extended byjava.util.Hashtable
          extended byorg.jgroups.blocks.DistributedHashtable
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, MembershipListener, MessageListener, java.io.Serializable

public class DistributedHashtable
extends java.util.Hashtable
implements MessageListener, MembershipListener, java.lang.Cloneable

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.

Both keys and values added to the hashtable must be serializable, 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 java.rmi.server.RemoteObject 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 DistributedHashtable thus allows to implement a distributed naming service in just a couple of lines.

An instance of this class will contact an existing member of the group to fetch its initial state (using the state exchange funclet StateExchangeFunclet.

Author:
Bela Ban, Alfonso Olias-Sanz
See Also:
Serialized Form

Nested Class Summary
static interface DistributedHashtable.Notification
           
 
Constructor Summary
DistributedHashtable(JChannel channel, boolean persistent, long state_timeout)
           
DistributedHashtable(JChannel channel, long state_timeout)
           
DistributedHashtable(java.lang.String groupname, ChannelFactory factory, java.lang.String properties, boolean persistent, long state_timeout)
          Creates a DisttributedHashtable.
DistributedHashtable(java.lang.String groupname, ChannelFactory factory, java.lang.String properties, long state_timeout)
          Creates a DistributedHashtable
 
Method Summary
 void _clear()
           
 java.lang.Object _put(java.lang.Object key, java.lang.Object value)
           
 void _putAll(java.util.Map m)
           
 java.lang.Object _remove(java.lang.Object key)
           
 void addNotifier(DistributedHashtable.Notification n)
           
 void block()
          Block sending and receiving of messages until ViewAccepted is called
 void clear()
          Clears this hashtable so that it contains no keys
 Channel getChannel()
           
 java.lang.String getGroupName()
           
 Address getLocalAddress()
           
 boolean getPersistent()
           
 byte[] getState()
           
protected  void init(long state_timeout)
           
static void main(java.lang.String[] args)
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Maps the specified key to the specified value in the hashtable.
 void putAll(java.util.Map m)
          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.
 void receive(Message msg)
           
 java.lang.Object remove(java.lang.Object key)
          Removes the key (and its corresponding value) from the Hashtable.
 void removeNotifier(DistributedHashtable.Notification n)
           
 void setPersistent(boolean p)
           
 void setState(byte[] new_state)
           
 void start(long state_timeout)
           
 void stop()
           
 void suspect(Address suspected_mbr)
          Called when a member is suspected
 void viewAccepted(View new_view)
          Called by JGroups to notify the target object of a change of membership.
 
Methods inherited from class java.util.Hashtable
clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, rehash, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DistributedHashtable

public DistributedHashtable(java.lang.String groupname,
                            ChannelFactory factory,
                            java.lang.String properties,
                            long state_timeout)
                     throws ChannelException
Creates a DistributedHashtable

Parameters:
groupname - The name of the group to join
factory - The ChannelFactory which will be used to create a channel
properties - The property string to be used to define the channel
state_timeout - The time to wait until state is retrieved in milliseconds. A value of 0 means wait forever.

DistributedHashtable

public DistributedHashtable(java.lang.String groupname,
                            ChannelFactory factory,
                            java.lang.String properties,
                            boolean persistent,
                            long state_timeout)
                     throws ChannelException
Creates a DisttributedHashtable. Optionally the contents can be saved to persistemt storage using the PersistenceManager.

Parameters:
groupname - Name of the group to join
factory - Instance of a ChannelFactory to create the channel
properties - Protocol stack properties
persistent - Whether the contents should be persisted
state_timeout - Max number of milliseconds to wait until state is retrieved

DistributedHashtable

public DistributedHashtable(JChannel channel,
                            long state_timeout)
                     throws ChannelNotConnectedException,
                            ChannelClosedException

DistributedHashtable

public DistributedHashtable(JChannel channel,
                            boolean persistent,
                            long state_timeout)
                     throws ChannelNotConnectedException,
                            ChannelClosedException
Method Detail

init

protected void init(long state_timeout)
             throws ChannelClosedException,
                    ChannelNotConnectedException
Throws:
ChannelClosedException
ChannelNotConnectedException

start

public void start(long state_timeout)
           throws ChannelClosedException,
                  ChannelNotConnectedException
Throws:
ChannelClosedException
ChannelNotConnectedException

getLocalAddress

public Address getLocalAddress()

getGroupName

public java.lang.String getGroupName()

getChannel

public Channel getChannel()

getPersistent

public boolean getPersistent()

setPersistent

public void setPersistent(boolean p)

addNotifier

public void addNotifier(DistributedHashtable.Notification n)

removeNotifier

public void removeNotifier(DistributedHashtable.Notification n)

stop

public void stop()

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Maps the specified key to the specified value in the hashtable. Neither of both parameters can be null

Specified by:
put in interface java.util.Map
Parameters:
key - - the hashtable key
value - - the value
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one

putAll

public void putAll(java.util.Map m)
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.

Specified by:
putAll in interface java.util.Map
Parameters:
m - - Mappings to be stored in this map

clear

public void clear()
Clears this hashtable so that it contains no keys

Specified by:
clear in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key (and its corresponding value) from the Hashtable.

Specified by:
remove in interface java.util.Map
Parameters:
key - - the key to be removed.
Returns:
the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping.

_put

public java.lang.Object _put(java.lang.Object key,
                             java.lang.Object value)

_putAll

public void _putAll(java.util.Map m)
See Also:
Map.putAll(java.util.Map)

_clear

public void _clear()

_remove

public java.lang.Object _remove(java.lang.Object key)

receive

public void receive(Message msg)
Specified by:
receive in interface MessageListener

getState

public byte[] getState()
Specified by:
getState in interface MessageListener

setState

public void setState(byte[] new_state)
Specified by:
setState in interface MessageListener

viewAccepted

public void viewAccepted(View new_view)
Description copied from interface: MembershipListener
Called by JGroups to notify the target object of a change of membership. No long running actions should be done in this callback in the case of Ensemble, as this would block Ensemble. If some long running action needs to be performed, it should be done in a separate thread (cf. ../Tests/QuoteServer.java).

Specified by:
viewAccepted in interface MembershipListener

suspect

public void suspect(Address suspected_mbr)
Called when a member is suspected

Specified by:
suspect in interface MembershipListener

block

public void block()
Block sending and receiving of messages until ViewAccepted is called

Specified by:
block in interface MembershipListener

main

public static void main(java.lang.String[] args)


Copyright © 2001,2002 www.jgroups.com . All Rights Reserved.