Package org.jgroups.blocks
Class ReplicatedHashMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.jgroups.blocks.ReplicatedHashMap<K,V>
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.util.concurrent.ConcurrentMap<K,V>
,java.util.Map<K,V>
,ReplicatedMap<K,V>
,Receiver
public class ReplicatedHashMap<K,V> extends java.util.AbstractMap<K,V> implements java.util.concurrent.ConcurrentMap<K,V>, Receiver, ReplicatedMap<K,V>, java.io.Closeable
Implementation of aConcurrentMap
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.
AReplicatedHashMap
allows one 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.
- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReplicatedHashMap.Notification<K,V>
-
Field Summary
Fields Modifier and Type Field Description protected RequestOptions
call_options
protected RpcDispatcher
disp
protected Log
log
protected java.util.concurrent.ConcurrentMap<K,V>
map
wrapped map instanceprotected static java.util.Map<java.lang.Short,java.lang.reflect.Method>
methods
-
Constructor Summary
Constructors Constructor Description ReplicatedHashMap(java.util.concurrent.ConcurrentMap<K,V> map, JChannel channel)
Constructs a new ReplicatedHashMap using provided map instance.ReplicatedHashMap(JChannel channel)
Constructs a new ReplicatedHashMap with channel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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(K key)
boolean
_remove(K key, V value)
V
_replace(K key, V value)
boolean
_replace(K key, V oldValue, V newValue)
void
addNotifier(ReplicatedHashMap.Notification n)
void
clear()
Removes all of the mappings from this map.void
close()
boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set<java.util.Map.Entry<K,V>>
entrySet()
V
get(java.lang.Object key)
JChannel
getChannel()
java.lang.String
getClusterName()
Address
getLocalAddress()
void
getState(java.io.OutputStream ostream)
Allows an application to write the state to an OutputStream.long
getTimeout()
The timeout (in milliseconds) for blocking updatesprotected 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)
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
setState(java.io.InputStream istream)
Allows an application to read the state from an InputStream.void
setTimeout(long timeout)
Sets the cluster call timeout (until all acks have been received)void
start(long state_timeout)
Fetches the statevoid
stop()
static <K,V>
ReplicatedMap<K,V>synchronizedMap(ReplicatedMap<K,V> map)
Creates a synchronized facade for a ReplicatedMap.void
viewAccepted(View new_view)
Called when a change in membership has occurred.-
Methods inherited from class java.util.AbstractMap
clone, equals, hashCode, isEmpty, keySet, size, toString, values
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
methods
protected static java.util.Map<java.lang.Short,java.lang.reflect.Method> methods
-
disp
protected RpcDispatcher disp
-
call_options
protected final RequestOptions call_options
-
log
protected final Log log
-
-
Constructor Detail
-
ReplicatedHashMap
public ReplicatedHashMap(JChannel channel)
Constructs a new ReplicatedHashMap with channel. Callstart(long)
to start this map.
-
-
Method Detail
-
init
protected final void init()
-
isBlockingUpdates
public boolean isBlockingUpdates()
-
setBlockingUpdates
public void setBlockingUpdates(boolean blocking_updates)
Whether updates across the cluster should be asynchronous (default) or synchronous)- Parameters:
blocking_updates
-
-
getTimeout
public long getTimeout()
The timeout (in milliseconds) for blocking updates
-
setTimeout
public void setTimeout(long timeout)
Sets the cluster call timeout (until all acks have been received)- Parameters:
timeout
- The timeout (in milliseconds) for blocking updates
-
start
public final void start(long state_timeout) throws java.lang.Exception
Fetches the state- Parameters:
state_timeout
-- Throws:
java.lang.Exception
-
getLocalAddress
public Address getLocalAddress()
-
getClusterName
public java.lang.String getClusterName()
-
getChannel
public JChannel getChannel()
-
addNotifier
public void addNotifier(ReplicatedHashMap.Notification n)
-
removeNotifier
public void removeNotifier(ReplicatedHashMap.Notification n)
-
stop
public void stop()
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
put
public V put(K key, V value)
Maps the specified key to the specified value in this table. Neither the key nor the value can be null.The value can be retrieved by calling the get method with a key that is equal to the original key.
- Specified by:
put
in interfacejava.util.Map<K,V>
- Overrides:
put
in classjava.util.AbstractMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with key, or null if there was no mapping for key
- Throws:
java.lang.NullPointerException
- if the specified key or value is null
-
putIfAbsent
public V putIfAbsent(K key, V value)
- Specified by:
putIfAbsent
in interfacejava.util.concurrent.ConcurrentMap<K,V>
- Specified by:
putIfAbsent
in interfacejava.util.Map<K,V>
- Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
java.lang.NullPointerException
- if the specified key or value is null
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified map.
-
clear
public void clear()
Removes all of the mappings from this map.
-
remove
public V remove(java.lang.Object key)
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.- Specified by:
remove
in interfacejava.util.Map<K,V>
- Overrides:
remove
in classjava.util.AbstractMap<K,V>
- Parameters:
key
- the key that needs to be removed- Returns:
- the previous value associated with key, or null if there was no mapping for key
- Throws:
java.lang.NullPointerException
- if the specified key is null
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
replace
public V replace(K key, V value)
- Specified by:
replace
in interfacejava.util.concurrent.ConcurrentMap<K,V>
- Specified by:
replace
in interfacejava.util.Map<K,V>
- Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
java.lang.NullPointerException
- if the specified key or value is null
-
_putIfAbsent
public V _putIfAbsent(K key, V value)
- Specified by:
_putIfAbsent
in interfaceReplicatedMap<K,V>
-
_putAll
public void _putAll(java.util.Map<? extends K,? extends V> map)
- Specified by:
_putAll
in interfaceReplicatedMap<K,V>
- See Also:
Map.putAll(java.util.Map)
-
_clear
public void _clear()
- Specified by:
_clear
in interfaceReplicatedMap<K,V>
-
_remove
public boolean _remove(K key, V value)
- Specified by:
_remove
in interfaceReplicatedMap<K,V>
-
_replace
public boolean _replace(K key, V oldValue, V newValue)
- Specified by:
_replace
in interfaceReplicatedMap<K,V>
-
getState
public void getState(java.io.OutputStream ostream) throws java.lang.Exception
Description copied from interface:Receiver
Allows an application to write the state to an OutputStream. After the state has been written, the OutputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
-
setState
public void setState(java.io.InputStream istream) throws java.lang.Exception
Description copied from interface:Receiver
Allows an application to read the state from an InputStream. After the state has been read, the InputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
-
viewAccepted
public void viewAccepted(View new_view)
Description copied from interface:Receiver
Called when a change in membership has occurred. No long running actions, sending of messages or anything that could block should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.
Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens whenJChannel.connect(String)
returns.- Specified by:
viewAccepted
in interfaceReceiver
-
synchronizedMap
public static <K,V> ReplicatedMap<K,V> synchronizedMap(ReplicatedMap<K,V> map)
Creates a synchronized facade for a ReplicatedMap. All methods which change state are invoked through a monitor. This is similar toCollections.synchronizedMap(Map)
, but also includes the replication methods (starting with an underscore).- Parameters:
map
-- Returns:
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
-