java.io.Serializable
, java.util.concurrent.ConcurrentMap<K,V>
, java.util.Map<K,V>
, ReplicatedMap<K,V>
, ExtendedMembershipListener
, ExtendedMessageListener
, ExtendedReceiver
, MembershipListener
, MessageListener
, Receiver
public class ReplicatedHashMap<K extends java.io.Serializable,V extends java.io.Serializable> extends java.util.concurrent.ConcurrentHashMap<K,V> implements ExtendedReceiver, ReplicatedMap<K,V>
ConcurrentHashMap
with replication of the contents across a cluster.
Any change to the hashmap (clear(), put(), remove() etc) will transparently be
propagated to all replicas in the group. All read-only methods will always access the local replica.
Keys and values added to the hashmap 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 ReplicatedHashMap
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.
This class combines both ReplicatedHashtable
(asynchronous replication) and
DistributedHashtable
(synchronous replication) into one class
Modifier and Type | Class | Description |
---|---|---|
static interface |
ReplicatedHashMap.Notification<K extends java.io.Serializable,V extends java.io.Serializable> |
java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,V extends java.lang.Object>
Modifier and Type | Field | Description |
---|---|---|
protected RpcDispatcher |
disp |
|
protected org.apache.commons.logging.Log |
log |
|
protected static java.util.Map<java.lang.Short,java.lang.reflect.Method> |
methods |
|
protected Promise<java.lang.Boolean> |
state_promise |
|
protected long |
timeout |
For blocking updates only: the max time to wait (0 == forever)
|
protected int |
update_mode |
Whether updates across the cluster should be asynchronous (default) or synchronous)
|
Constructor | Description |
---|---|
ReplicatedHashMap(java.lang.String clustername,
ChannelFactory factory,
java.lang.String properties,
boolean persistent,
long state_timeout) |
Creates a ReplicatedHashMap.
|
ReplicatedHashMap(java.lang.String clustername,
ChannelFactory factory,
java.lang.String properties,
long state_timeout) |
Creates a ReplicatedHashMap
|
ReplicatedHashMap(Channel channel) |
|
ReplicatedHashMap(Channel channel,
boolean persistent) |
Modifier and Type | Method | Description |
---|---|---|
void |
_clear() |
|
V |
_put(K key,
V value) |
|
void |
_putAll(java.util.Map<? extends K,? extends V> map) |
|
V |
_putIfAbsent(K key,
V value) |
|
V |
_remove(java.lang.Object key) |
|
boolean |
_remove(java.lang.Object key,
java.lang.Object value) |
|
V |
_replace(K key,
V value) |
|
boolean |
_replace(K key,
V oldValue,
V newValue) |
|
void |
addNotifier(ReplicatedHashMap.Notification n) |
|
void |
block() |
Block sending and receiving of messages until ViewAccepted is called
|
void |
clear() |
Removes all of the mappings from this map.
|
Channel |
getChannel() |
|
java.lang.String |
getClusterName() |
|
Address |
getLocalAddress() |
|
boolean |
getPersistent() |
|
byte[] |
getState() |
Answers the group state; e.g., when joining.
|
void |
getState(java.io.OutputStream ostream) |
Allows an application to write a state through a provided OutputStream.
|
byte[] |
getState(java.lang.String state_id) |
Allows an application to provide a partial state as a byte array
|
void |
getState(java.lang.String state_id,
java.io.OutputStream ostream) |
Allows an application to write a partial state through a provided OutputStream.
|
long |
getTimeout() |
The timeout (in milliseconds) for blocking updates
|
protected void |
init() |
|
boolean |
isBlockingUpdates() |
|
V |
put(K key,
V value) |
Maps the specified key to the specified value in this table.
|
void |
putAll(java.util.Map<? extends K,? extends V> m) |
Copies all of the mappings from the specified map to this one.
|
V |
putIfAbsent(K key,
V value) |
|
void |
receive(Message msg) |
Called when a message is received.
|
V |
remove(java.lang.Object key) |
Removes the key (and its corresponding value) from this map.
|
boolean |
remove(java.lang.Object key,
java.lang.Object value) |
|
void |
removeNotifier(ReplicatedHashMap.Notification n) |
|
V |
replace(K key,
V value) |
|
boolean |
replace(K key,
V oldValue,
V newValue) |
|
void |
setBlockingUpdates(boolean blocking_updates) |
Whether updates across the cluster should be asynchronous (default) or synchronous)
|
void |
setDeadlockDetection(boolean flag) |
|
void |
setPersistent(boolean p) |
|
void |
setState(byte[] new_state) |
Sets the group state; e.g., when joining.
|
void |
setState(java.io.InputStream istream) |
Allows an application to read a state through a provided InputStream.
|
void |
setState(java.lang.String state_id,
byte[] state) |
Allows an application to read a partial state indicated by state_id from
a given state byte array parameter.
|
void |
setState(java.lang.String state_id,
java.io.InputStream istream) |
Allows an application to read a partial state through a provided InputStream.
|
void |
setTimeout(long timeout) |
Sets the cluster call timeout (until all acks have been received)
|
void |
start(long state_timeout) |
Fetches the state
|
void |
stop() |
|
void |
suspect(Address suspected_mbr) |
Called when a member is suspected
|
static <K extends java.io.Serializable,V extends java.io.Serializable> |
synchronizedMap(ReplicatedMap<K,V> map) |
Creates a synchronized facade for a ReplicatedMap.
|
void |
unblock() |
Called after the FLUSH protocol has unblocked previously blocked senders, and messages can be sent again.
|
void |
viewAccepted(View new_view) |
Called when a change in membership has occurred.
|
compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, get, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, replaceAll, search, searchEntries, searchKeys, searchValues, size, toString, values
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
protected static java.util.Map<java.lang.Short,java.lang.reflect.Method> methods
protected transient RpcDispatcher disp
protected final transient Promise<java.lang.Boolean> state_promise
protected int update_mode
protected long timeout
protected final org.apache.commons.logging.Log log
public ReplicatedHashMap(java.lang.String clustername, ChannelFactory factory, java.lang.String properties, long state_timeout) throws ChannelException
clustername
- The name of the group to joinfactory
- The ChannelFactory which will be used to create a channelproperties
- The property string to be used to define the channel. This will override the properties of
the factory. If null, then the factory properties will be usedstate_timeout
- The time to wait until state is retrieved in milliseconds. A value of 0 means wait forever.ChannelException
public ReplicatedHashMap(java.lang.String clustername, ChannelFactory factory, java.lang.String properties, boolean persistent, long state_timeout) throws ChannelException
PersistenceManager
.clustername
- Name of the group to joinfactory
- Instance of a ChannelFactory to create the channelproperties
- Protocol stack properties. This will override the properties of the factory. If
null, then the factory properties will be usedpersistent
- Whether the contents should be persistedstate_timeout
- Max number of milliseconds to wait until the state is retrievedChannelException
public ReplicatedHashMap(Channel channel)
public ReplicatedHashMap(Channel channel, boolean persistent)
protected final void init()
public boolean isBlockingUpdates()
public void setBlockingUpdates(boolean blocking_updates)
blocking_updates
- public long getTimeout()
public void setTimeout(long timeout)
timeout
- The timeout (in milliseconds) for blocking updatespublic final void start(long state_timeout) throws ChannelClosedException, ChannelNotConnectedException
state_timeout
- ChannelClosedException
ChannelNotConnectedException
public Address getLocalAddress()
public java.lang.String getClusterName()
public Channel getChannel()
public boolean getPersistent()
public void setPersistent(boolean p)
public void setDeadlockDetection(boolean flag)
public void addNotifier(ReplicatedHashMap.Notification n)
public void removeNotifier(ReplicatedHashMap.Notification n)
public void stop()
public V put(K key, V value)
The value can be retrieved by calling the get method with a key that is equal to the original key.
put
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
put
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyjava.lang.NullPointerException
- if the specified key or value is nullpublic V putIfAbsent(K key, V value)
putIfAbsent
in interface java.util.concurrent.ConcurrentMap<K extends java.io.Serializable,V extends java.io.Serializable>
putIfAbsent
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
putIfAbsent
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
java.lang.NullPointerException
- if the specified key or value is nullpublic void putAll(java.util.Map<? extends K,? extends V> m)
public void clear()
public V remove(java.lang.Object key)
remove
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
remove
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
key
- the key that needs to be removedjava.lang.NullPointerException
- if the specified key is nullpublic boolean remove(java.lang.Object key, java.lang.Object value)
remove
in interface java.util.concurrent.ConcurrentMap<K extends java.io.Serializable,V extends java.io.Serializable>
remove
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
remove
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
java.lang.NullPointerException
- if the specified key is nullpublic boolean replace(K key, V oldValue, V newValue)
replace
in interface java.util.concurrent.ConcurrentMap<K extends java.io.Serializable,V extends java.io.Serializable>
replace
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
replace
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
java.lang.NullPointerException
- if any of the arguments are nullpublic V replace(K key, V value)
replace
in interface java.util.concurrent.ConcurrentMap<K extends java.io.Serializable,V extends java.io.Serializable>
replace
in interface java.util.Map<K extends java.io.Serializable,V extends java.io.Serializable>
replace
in class java.util.concurrent.ConcurrentHashMap<K extends java.io.Serializable,V extends java.io.Serializable>
java.lang.NullPointerException
- if the specified key or value is nullpublic V _put(K key, V value)
_put
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public V _putIfAbsent(K key, V value)
_putIfAbsent
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public void _putAll(java.util.Map<? extends K,? extends V> map)
_putAll
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
Map.putAll(java.util.Map)
public void _clear()
_clear
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public V _remove(java.lang.Object key)
_remove
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public boolean _remove(java.lang.Object key, java.lang.Object value)
_remove
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public boolean _replace(K key, V oldValue, V newValue)
_replace
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public V _replace(K key, V value)
_replace
in interface ReplicatedMap<K extends java.io.Serializable,V extends java.io.Serializable>
public void receive(Message msg)
MessageListener
receive
in interface MessageListener
public byte[] getState()
MessageListener
getState
in interface MessageListener
public void setState(byte[] new_state)
MessageListener
setState
in interface MessageListener
public void viewAccepted(View new_view)
MembershipListener
Channel.connect(String)
returns.viewAccepted
in interface MembershipListener
public void suspect(Address suspected_mbr)
suspect
in interface MembershipListener
public void block()
block
in interface MembershipListener
public byte[] getState(java.lang.String state_id)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedpublic void getState(java.io.OutputStream ostream)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
ostream
- the OutputStreamOutputStream.close()
public void getState(java.lang.String state_id, java.io.OutputStream ostream)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedostream
- the OutputStreamOutputStream.close()
public void setState(java.lang.String state_id, byte[] state)
ExtendedMessageListener
setState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedstate
- partial state for the given state_idpublic void setState(java.io.InputStream istream)
ExtendedMessageListener
setState
in interface ExtendedMessageListener
istream
- the InputStreamInputStream.close()
public void setState(java.lang.String state_id, java.io.InputStream istream)
ExtendedMessageListener
setState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedistream
- the InputStreamInputStream.close()
public void unblock()
ExtendedMembershipListener
unblock
in interface ExtendedMembershipListener
public static <K extends java.io.Serializable,V extends java.io.Serializable> ReplicatedMap<K,V> synchronizedMap(ReplicatedMap<K,V> map)
map
- Copyright ? 1998-2008 Bela Ban. All Rights Reserved.