public class ReplicatedHashMap<K extends Serializable,V extends Serializable> extends AbstractMap<K,V> implements ConcurrentMap<K,V>, 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 org.jgroups.blocks.ReplicatedHashtable
(asynchronous replication) and
org.jgroups.blocks.DistributedHashtable
(synchronous replication)
into one class
Modifier and Type | Class and Description |
---|---|
static interface |
ReplicatedHashMap.Notification<K extends Serializable,V extends Serializable> |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected RpcDispatcher |
disp |
protected Log |
log |
protected ConcurrentMap<K,V> |
map
Wrapped map instance.
|
protected static Map<Short,Method> |
methods |
protected Promise<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 and Description |
---|
ReplicatedHashMap(Channel channel)
Constructs a new ReplicatedHashMap with channel.
|
ReplicatedHashMap(Channel channel,
boolean persistent)
Constructs a new ReplicatedHashMap with channel and persistence option.
|
ReplicatedHashMap(ConcurrentMap<K,V> map,
Channel channel,
boolean persistent)
Constructs a new ReplicatedHashMap using provided map instance.
|
ReplicatedHashMap(String clustername,
ChannelFactory factory,
String properties,
boolean persistent,
long state_timeout)
Creates a ReplicatedHashMap.
|
ReplicatedHashMap(String clustername,
ChannelFactory factory,
String properties,
long state_timeout)
Creates a ReplicatedHashMap
|
Modifier and Type | Method and Description |
---|---|
void |
_clear() |
V |
_put(K key,
V value) |
void |
_putAll(Map<? extends K,? extends V> map) |
V |
_putIfAbsent(K key,
V value) |
V |
_remove(Object key) |
boolean |
_remove(Object key,
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.
|
boolean |
contains(Object value)
Deprecated.
|
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Enumeration<V> |
elements()
Deprecated.
|
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key) |
Channel |
getChannel() |
String |
getClusterName() |
Address |
getLocalAddress() |
boolean |
getPersistent() |
byte[] |
getState()
Answers the group state; e.g., when joining.
|
void |
getState(OutputStream ostream)
Allows an application to write a state through a provided OutputStream.
|
byte[] |
getState(String state_id)
Allows an application to provide a partial state as a byte array
|
void |
getState(String state_id,
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() |
Enumeration<K> |
keys()
Deprecated.
|
Set<K> |
keySet() |
V |
put(K key,
V value)
Maps the specified key to the specified value in this table.
|
void |
putAll(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(Object key)
Removes the key (and its corresponding value) from this map.
|
boolean |
remove(Object key,
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(InputStream istream)
Allows an application to read a state through a provided InputStream.
|
void |
setState(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(String state_id,
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)
|
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 extends Serializable,V extends 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.
|
Collection<V> |
values() |
void |
viewAccepted(View new_view)
Called when a change in membership has occurred.
|
protected RpcDispatcher disp
protected int update_mode
protected long timeout
protected final Log log
protected ConcurrentMap<K extends Serializable,V extends Serializable> map
public ReplicatedHashMap(String clustername, ChannelFactory factory, 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(String clustername, ChannelFactory factory, 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)
start(long)
to
start this map.public ReplicatedHashMap(Channel channel, boolean persistent)
start(long)
to start this map.public ReplicatedHashMap(ConcurrentMap<K,V> map, 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 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 Map<K extends Serializable,V extends Serializable>
put
in class AbstractMap<K extends Serializable,V extends Serializable>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyNullPointerException
- if the specified key or value is nullpublic V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K extends Serializable,V extends Serializable>
NullPointerException
- if the specified key or value is nullpublic void putAll(Map<? extends K,? extends V> m)
putAll
in interface Map<K extends Serializable,V extends Serializable>
putAll
in class AbstractMap<K extends Serializable,V extends Serializable>
m
- mappings to be stored in this mappublic void clear()
clear
in interface Map<K extends Serializable,V extends Serializable>
clear
in class AbstractMap<K extends Serializable,V extends Serializable>
public V remove(Object key)
remove
in interface Map<K extends Serializable,V extends Serializable>
remove
in class AbstractMap<K extends Serializable,V extends Serializable>
key
- the key that needs to be removedNullPointerException
- if the specified key is nullpublic boolean remove(Object key, Object value)
remove
in interface ConcurrentMap<K extends Serializable,V extends Serializable>
NullPointerException
- if the specified key is nullpublic boolean replace(K key, V oldValue, V newValue)
replace
in interface ConcurrentMap<K extends Serializable,V extends Serializable>
NullPointerException
- if any of the arguments are nullpublic V replace(K key, V value)
replace
in interface ConcurrentMap<K extends Serializable,V extends Serializable>
NullPointerException
- if the specified key or value is nullpublic V _put(K key, V value)
_put
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public V _putIfAbsent(K key, V value)
_putIfAbsent
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public void _putAll(Map<? extends K,? extends V> map)
_putAll
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
Map.putAll(java.util.Map)
public void _clear()
_clear
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public V _remove(Object key)
_remove
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public boolean _remove(Object key, Object value)
_remove
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public boolean _replace(K key, V oldValue, V newValue)
_replace
in interface ReplicatedMap<K extends Serializable,V extends Serializable>
public V _replace(K key, V value)
_replace
in interface ReplicatedMap<K extends Serializable,V extends 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 byte[] getState(String state_id)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedpublic void getState(OutputStream ostream)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
ostream
- the OutputStreamOutputStream.close()
public void getState(String state_id, OutputStream ostream)
ExtendedMessageListener
getState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedostream
- the OutputStreamOutputStream.close()
public void setState(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(InputStream istream)
ExtendedMessageListener
setState
in interface ExtendedMessageListener
istream
- the InputStreamInputStream.close()
public void setState(String state_id, InputStream istream)
ExtendedMessageListener
setState
in interface ExtendedMessageListener
state_id
- id of the partial state requestedistream
- the InputStreamInputStream.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()
ExtendedMembershipListener
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 ExtendedMembershipListener
public static <K extends Serializable,V extends Serializable> ReplicatedMap<K,V> synchronizedMap(ReplicatedMap<K,V> map)
map
- public boolean containsKey(Object key)
containsKey
in interface Map<K extends Serializable,V extends Serializable>
containsKey
in class AbstractMap<K extends Serializable,V extends Serializable>
public boolean containsValue(Object value)
containsValue
in interface Map<K extends Serializable,V extends Serializable>
containsValue
in class AbstractMap<K extends Serializable,V extends Serializable>
public Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K extends Serializable,V extends Serializable>
entrySet
in class AbstractMap<K extends Serializable,V extends Serializable>
public V get(Object key)
get
in interface Map<K extends Serializable,V extends Serializable>
get
in class AbstractMap<K extends Serializable,V extends Serializable>
public Set<K> keySet()
keySet
in interface Map<K extends Serializable,V extends Serializable>
keySet
in class AbstractMap<K extends Serializable,V extends Serializable>
public int size()
size
in interface Map<K extends Serializable,V extends Serializable>
size
in class AbstractMap<K extends Serializable,V extends Serializable>
public Collection<V> values()
values
in interface Map<K extends Serializable,V extends Serializable>
values
in class AbstractMap<K extends Serializable,V extends Serializable>
public boolean contains(Object value)
public Enumeration<K> keys()
public Enumeration<V> elements()
Copyright © 2013 JBoss, a division of Red Hat. All Rights Reserved.