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
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.
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.
Modifier and Type | Class and Description |
---|---|
static interface |
ReplicatedHashMap.Notification<K,V> |
Modifier and Type | Field and Description |
---|---|
protected RequestOptions |
call_options |
protected RpcDispatcher |
disp |
protected Log |
log |
protected java.util.concurrent.ConcurrentMap<K,V> |
map
wrapped map instance
|
protected static java.util.Map<java.lang.Short,java.lang.reflect.Method> |
methods |
Constructor and Description |
---|
ReplicatedHashMap(Channel channel)
Constructs a new ReplicatedHashMap with channel.
|
ReplicatedHashMap(java.util.concurrent.ConcurrentMap<K,V> map,
Channel channel)
Constructs a new ReplicatedHashMap using provided map instance.
|
Modifier and Type | Method and 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.
|
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) |
Channel |
getChannel() |
java.lang.String |
getClusterName() |
Address |
getLocalAddress() |
void |
getState(java.io.OutputStream ostream)
Allows an application to write a state through a provided OutputStream.
|
long |
getTimeout()
The timeout (in milliseconds) for blocking updates
|
protected void |
init() |
boolean |
isBlockingUpdates() |
java.util.Set<K> |
keySet() |
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 |
setState(java.io.InputStream istream)
Allows an application to read a state through a provided InputStream.
|
void |
setTimeout(long timeout)
Sets the cluster call timeout (until all acks have been received)
|
int |
size() |
void |
start(long state_timeout)
Fetches the state
|
void |
stop() |
void |
suspect(Address suspected_mbr)
Called when a member is suspected
|
static <K,V> ReplicatedMap<K,V> |
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.
|
java.util.Collection<V> |
values() |
void |
viewAccepted(View new_view)
Called when a change in membership has occurred.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
protected static java.util.Map<java.lang.Short,java.lang.reflect.Method> methods
protected RpcDispatcher disp
protected final RequestOptions call_options
protected final Log log
public ReplicatedHashMap(Channel channel)
start(long)
to start this map.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 java.lang.Exception
state_timeout
- java.lang.Exception
public Address getLocalAddress()
public java.lang.String getClusterName()
public Channel getChannel()
public void addNotifier(ReplicatedHashMap.Notification n)
public void removeNotifier(ReplicatedHashMap.Notification n)
public void stop()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
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,V>
put
in class java.util.AbstractMap<K,V>
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,V>
putIfAbsent
in interface java.util.Map<K,V>
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,V>
remove
in class java.util.AbstractMap<K,V>
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)
public V replace(K key, V value)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
replace
in interface java.util.Map<K,V>
java.lang.NullPointerException
- if the specified key or value is nullpublic V _putIfAbsent(K key, V value)
_putIfAbsent
in interface ReplicatedMap<K,V>
public void _putAll(java.util.Map<? extends K,? extends V> map)
_putAll
in interface ReplicatedMap<K,V>
Map.putAll(java.util.Map)
public void _clear()
_clear
in interface ReplicatedMap<K,V>
public V _remove(java.lang.Object key)
_remove
in interface ReplicatedMap<K,V>
public boolean _remove(java.lang.Object key, java.lang.Object value)
_remove
in interface ReplicatedMap<K,V>
public boolean _replace(K key, V oldValue, V newValue)
_replace
in interface ReplicatedMap<K,V>
public void receive(Message msg)
MessageListener
receive
in interface MessageListener
public void getState(java.io.OutputStream ostream) throws java.lang.Exception
MessageListener
getState
in interface MessageListener
ostream
- the OutputStreamjava.lang.Exception
- if the streaming fails, any exceptions should be thrown so that the state requester
can re-throw them and let the caller know what happenedOutputStream.close()
public void setState(java.io.InputStream istream) throws java.lang.Exception
MessageListener
setState
in interface MessageListener
istream
- the InputStreamjava.lang.Exception
- if the streaming fails, any exceptions should be thrown so that the state requester
can catch them and thus know what happenedInputStream.close()
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 void unblock()
MembershipListener
Note that during new view installation we provide guarantee that unblock invocation strictly follows view installation at some node A belonging to that view . However, some other message M may squeeze in between view and unblock callbacks. For more details see https://jira.jboss.org/jira/browse/JGRP-986
unblock
in interface MembershipListener
public static <K,V> ReplicatedMap<K,V> synchronizedMap(ReplicatedMap<K,V> map)
Collections.synchronizedMap(Map)
, but also includes the replication
methods (starting with an underscore).map
- public boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public V get(java.lang.Object key)
public java.util.Set<K> keySet()
public int size()
Copyright © 1998-2020 Red Hat. All Rights Reserved.