org.jgroups.blocks

Class ReplicatedHashtable

Implemented Interfaces:
MembershipListener, MessageListener

public class ReplicatedHashtable
extends Hashtable
implements MessageListener, MembershipListener

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 ReplicatedHashtable 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.

Contrary to DistributedHashtable, this class does not make use of RpcDispatcher (and RequestCorrelator) but uses plain asynchronous messages instead.

Authors:
Bela Ban
Alfonso Olias-Sanz
To do:
implement putAll() [similar to DistributedHashtable].

Nested Class Summary

static interface
ReplicatedHashtable.Notification
static interface
ReplicatedHashtable.StateTransferListener

Field Summary

protected Log
log

Constructor Summary

ReplicatedHashtable(String groupname, ChannelFactory factory, String properties, long state_timeout)
ReplicatedHashtable(String groupname, ChannelFactory factory, ReplicatedHashtable.StateTransferListener l, String properties, long state_timeout)
Creates a ReplicatedHashtable
ReplicatedHashtable(JChannel channel, long state_timeout)
ReplicatedHashtable(JChannel channel, ReplicatedHashtable.StateTransferListener l, long state_timeout)

Method Summary

void
_putAll(Map m)
void
addNotifier(ReplicatedHashtable.Notification n)
void
addStateTransferListener(ReplicatedHashtable.StateTransferListener l)
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()
Address
getLocalAddress()
byte[]
getState()
Answers the group state; e.g., when joining.
Object
put(Object key, Object value)
Maps the specified key to the specified value in the hashtable.
void
putAll(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)
Called when a message is received.
Object
remove(Object key)
Removes the key (and its corresponding value) from the Hashtable.
void
removeStateTransferListener(ReplicatedHashtable.StateTransferListener l)
void
setState(byte[] new_state)
Sets the group state; e.g., when joining.
boolean
stateTransferRunning()
void
suspect(Address suspected_mbr)
Called when a member is suspected
void
viewAccepted(View new_view)
Called when a change in membership has occurred.

Field Details

log

protected final Log log

Constructor Details

ReplicatedHashtable

public ReplicatedHashtable(String groupname,
                           ChannelFactory factory,
                           String properties,
                           long state_timeout)

ReplicatedHashtable

public ReplicatedHashtable(String groupname,
                           ChannelFactory factory,
                           ReplicatedHashtable.StateTransferListener l,
                           String properties,
                           long state_timeout)
Creates a ReplicatedHashtable
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.

ReplicatedHashtable

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

ReplicatedHashtable

public ReplicatedHashtable(JChannel channel,
                           ReplicatedHashtable.StateTransferListener l,
                           long state_timeout)
            throws ChannelClosedException,
                   ChannelNotConnectedException

Method Details

_putAll

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

addNotifier

public void addNotifier(ReplicatedHashtable.Notification n)

addStateTransferListener

public void addStateTransferListener(ReplicatedHashtable.StateTransferListener l)

block

public void block()
Block sending and receiving of messages until ViewAccepted is called
Specified by:
block in interface MembershipListener

clear

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

getChannel

public Channel getChannel()

getLocalAddress

public Address getLocalAddress()

getState

public byte[] getState()
Answers the group state; e.g., when joining.
Specified by:
getState in interface MessageListener
Returns:
byte[]

put

public Object put(Object key,
                  Object value)
Maps the specified key to the specified value in the hashtable. Neither of both parameters can be null
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(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.
Parameters:
m - - Mappings to be stored in this map

receive

public void receive(Message msg)
Called when a message is received.
Specified by:
receive in interface MessageListener
Parameters:
msg -

remove

public Object remove(Object key)
Removes the key (and its corresponding value) from the Hashtable.
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.

removeStateTransferListener

public void removeStateTransferListener(ReplicatedHashtable.StateTransferListener l)

setState

public void setState(byte[] new_state)
Sets the group state; e.g., when joining.
Specified by:
setState in interface MessageListener
Parameters:

stateTransferRunning

public boolean stateTransferRunning()

suspect

public void suspect(Address suspected_mbr)
Called when a member is suspected
Specified by:
suspect in interface MembershipListener

viewAccepted

public void viewAccepted(View new_view)
Called when a change in membership has occurred. No long running actions should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.
Specified by:
viewAccepted in interface MembershipListener

Copyright B) 1998-2005 Bela Ban. All Rights Reserved.