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:
Closeable,AutoCloseable,ConcurrentMap<K,,V> Map<K,,V> ReplicatedMap<K,,V> Receiver
public class ReplicatedHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Receiver, ReplicatedMap<K,V>, Closeable
Implementation of a
ConcurrentMap 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.
A ReplicatedHashMap 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceprivate static final classNested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final RequestOptionsprivate final JChannelprivate static final shortprivate Stringprotected RpcDispatcherprotected final Logprotected ConcurrentMap<K, V> wrapped map instanceprivate final Set<ReplicatedHashMap.Notification> private static final shortprivate static final shortprivate static final shortprivate static final shortprivate static final shortprivate static final shortprivate static final short -
Constructor Summary
ConstructorsConstructorDescriptionReplicatedHashMap(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
Modifier and TypeMethodDescriptionvoid_clear()void_putIfAbsent(K key, V value) booleanbooleanvoidvoidclear()Removes all of the mappings from this map.voidclose()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()voidgetState(OutputStream ostream) Allows an application to write the state to an OutputStream.longThe timeout (in milliseconds) for blocking updatesprotected final voidinit()booleanMaps the specified key to the specified value in this table.voidCopies all of the mappings from the specified map to this one.putIfAbsent(K key, V value) Removes the key (and its corresponding value) from this map.booleanvoidboolean(package private) voidvoidsetBlockingUpdates(boolean blocking_updates) Whether updates across the cluster should be asynchronous (default) or synchronous)voidsetState(InputStream istream) Allows an application to read the state from an InputStream.voidsetTimeout(long timeout) Sets the cluster call timeout (until all acks have been received)final voidstart(long state_timeout) Fetches the statevoidstop()static <K,V> ReplicatedMap <K, V> synchronizedMap(ReplicatedMap<K, V> map) Creates a synchronized facade for a ReplicatedMap.voidviewAccepted(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, valuesMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Field Details
-
PUT
private static final short PUT- See Also:
-
PUT_IF_ABSENT
private static final short PUT_IF_ABSENT- See Also:
-
PUT_ALL
private static final short PUT_ALL- See Also:
-
REMOVE
private static final short REMOVE- See Also:
-
REMOVE_IF_EQUALS
private static final short REMOVE_IF_EQUALS- See Also:
-
REPLACE_IF_EXISTS
private static final short REPLACE_IF_EXISTS- See Also:
-
REPLACE_IF_EQUALS
private static final short REPLACE_IF_EQUALS- See Also:
-
CLEAR
private static final short CLEAR- See Also:
-
methods
-
channel
-
disp
-
cluster_name
-
notifs
-
members
-
call_options
-
log
-
map
wrapped map instance
-
-
Constructor Details
-
ReplicatedHashMap
Constructs a new ReplicatedHashMap with channel. Callstart(long)to start this map. -
ReplicatedHashMap
Constructs a new ReplicatedHashMap using provided map instance.
-
-
Method Details
-
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
Fetches the state- Parameters:
state_timeout-- Throws:
Exception
-
getLocalAddress
-
getClusterName
-
getChannel
-
addNotifier
-
removeNotifier
-
stop
public void stop() -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
put
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:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<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:
NullPointerException- if the specified key or value is null
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
putIfAbsentin interfaceMap<K,V> - Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
NullPointerException- if the specified key or value is null
-
putAll
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
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.- Specified by:
removein interfaceMap<K,V> - Overrides:
removein classAbstractMap<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:
NullPointerException- if the specified key is null
-
remove
- Specified by:
removein interfaceConcurrentMap<K,V> - Specified by:
removein interfaceMap<K,V> - Throws:
NullPointerException- if the specified key is null
-
replace
- Specified by:
replacein interfaceConcurrentMap<K,V> - Specified by:
replacein interfaceMap<K,V> - Throws:
NullPointerException- if any of the arguments are null
-
replace
- Specified by:
replacein interfaceConcurrentMap<K,V> - Specified by:
replacein interfaceMap<K,V> - Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
NullPointerException- if the specified key or value is null
-
_put
- Specified by:
_putin interfaceReplicatedMap<K,V>
-
_putIfAbsent
- Specified by:
_putIfAbsentin interfaceReplicatedMap<K,V>
-
_putAll
- Specified by:
_putAllin interfaceReplicatedMap<K,V> - See Also:
-
_clear
public void _clear()- Specified by:
_clearin interfaceReplicatedMap<K,V>
-
_remove
- Specified by:
_removein interfaceReplicatedMap<K,V>
-
_remove
- Specified by:
_removein interfaceReplicatedMap<K,V>
-
_replace
- Specified by:
_replacein interfaceReplicatedMap<K,V>
-
_replace
- Specified by:
_replacein interfaceReplicatedMap<K,V>
-
getState
Description copied from interface:ReceiverAllows 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
Description copied from interface:ReceiverAllows 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
Description copied from interface:ReceiverCalled 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 when
JChannel.connect(String)returns.- Specified by:
viewAcceptedin interfaceReceiver
-
sendViewChangeNotifications
-
synchronizedMap
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
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V>
-
entrySet
-
get
-