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.

  • Field Details

  • Constructor Details

    • ReplicatedHashMap

      public ReplicatedHashMap(JChannel channel)
      Constructs a new ReplicatedHashMap with channel. Call start(long) to start this map.
    • ReplicatedHashMap

      public ReplicatedHashMap(ConcurrentMap<K,V> map, JChannel channel)
      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

      public final void start(long state_timeout) throws Exception
      Fetches the state
      Parameters:
      state_timeout -
      Throws:
      Exception
    • getLocalAddress

      public Address getLocalAddress()
    • getClusterName

      public 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 IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      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 interface Map<K,V>
      Overrides:
      put in class AbstractMap<K,V>
      Parameters:
      key - key with which the specified value is to be associated
      value - 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

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface ConcurrentMap<K,V>
      Specified by:
      putIfAbsent in interface Map<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

      public void putAll(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.
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class AbstractMap<K,V>
      Parameters:
      m - mappings to be stored in this map
    • clear

      public void clear()
      Removes all of the mappings from this map.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class AbstractMap<K,V>
    • remove

      public V remove(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 interface Map<K,V>
      Overrides:
      remove in class 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:
      NullPointerException - if the specified key is null
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface ConcurrentMap<K,V>
      Specified by:
      remove in interface Map<K,V>
      Throws:
      NullPointerException - if the specified key is null
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<K,V>
      Throws:
      NullPointerException - if any of the arguments are null
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface ConcurrentMap<K,V>
      Specified by:
      replace in interface Map<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

      public V _put(K key, V value)
      Specified by:
      _put in interface ReplicatedMap<K,V>
    • _putIfAbsent

      public V _putIfAbsent(K key, V value)
      Specified by:
      _putIfAbsent in interface ReplicatedMap<K,V>
    • _putAll

      public void _putAll(Map<? extends K,? extends V> map)
      Specified by:
      _putAll in interface ReplicatedMap<K,V>
      See Also:
    • _clear

      public void _clear()
      Specified by:
      _clear in interface ReplicatedMap<K,V>
    • _remove

      public V _remove(K key)
      Specified by:
      _remove in interface ReplicatedMap<K,V>
    • _remove

      public boolean _remove(K key, V value)
      Specified by:
      _remove in interface ReplicatedMap<K,V>
    • _replace

      public boolean _replace(K key, V oldValue, V newValue)
      Specified by:
      _replace in interface ReplicatedMap<K,V>
    • _replace

      public V _replace(K key, V value)
      Specified by:
      _replace in interface ReplicatedMap<K,V>
    • getState

      public void getState(OutputStream ostream) throws 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.
      Specified by:
      getState in interface Receiver
      Parameters:
      ostream - The OutputStream
      Throws:
      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 happened
    • setState

      public void setState(InputStream istream) throws 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.
      Specified by:
      setState in interface Receiver
      Parameters:
      istream - The InputStream
      Throws:
      Exception - If the streaming fails, any exceptions should be thrown so that the state requester can catch them and thus know what happened
    • 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 when JChannel.connect(String) returns.

      Specified by:
      viewAccepted in interface Receiver
    • sendViewChangeNotifications

      void sendViewChangeNotifications(View view, List<Address> new_mbrs, List<Address> old_mbrs)
    • 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 to Collections.synchronizedMap(Map), but also includes the replication methods (starting with an underscore).
      Parameters:
      map -
      Returns:
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class AbstractMap<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class AbstractMap<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class AbstractMap<K,V>