ehcache

net.sf.ehcache.distribution.jgroups
Class JGroupsCacheReplicator

java.lang.Object
  extended by net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicator
All Implemented Interfaces:
java.lang.Cloneable, CacheReplicator, CacheEventListener

public class JGroupsCacheReplicator
extends java.lang.Object
implements CacheReplicator

Version:
$Id: JGroupsCacheReplicator.java 592 2008-03-17 08:39:56Z gregluck $

This implements CacheReplicator using JGroups as underlying replication mechanism The peer provider should be of type JGroupsCacheManagerPeerProvider It is assumed that the cachepeer is a JGroupManager

Author:
Pierre Monestie (pmonestie[at]@gmail.com), Greg Luck

Field Summary
static long DEFAULT_ASYNC_INTERVAL
          Teh default interval for async cache replication
 
Constructor Summary
JGroupsCacheReplicator(boolean replicatePuts, boolean replicateUpdates, boolean replicateUpdatesViaCopy, boolean replicateRemovals, boolean replicateAsync)
          Constructor called by factory
 
Method Summary
 boolean alive()
          Checks that the replicator is is STATUS_ALIVE.
 java.lang.Object clone()
          Creates a clone of this listener.
 void dispose()
          Give the listener a chance to cleanup and free resources when no longer needed
 long getAsynchronousReplicationInterval()
          Get the time interval is ms between asynchronous replication
 boolean isReplicateAsync()
          Weather or not the cache is replicated asynchronously.
 boolean isReplicatePuts()
          Wether or not puts are replicated
 boolean isReplicateRemovals()
          wether or not removals are replicated
 boolean isReplicateUpdates()
          Wether or not updates are replicated
 boolean isReplicateUpdatesViaCopy()
          Returns whether update is through copy or invalidate
(package private) static java.util.List listRemoteCachePeers(Ehcache cache)
          Package protected List of cache peers
 boolean notAlive()
          Returns whether the replicator is not active.
 void notifyElementEvicted(Ehcache cache, Element element)
          Called immediately after an element is evicted from the cache.
 void notifyElementExpired(Ehcache cache, Element element)
          Called immediately after an element is found to be expired.
 void notifyElementPut(Ehcache cache, Element element)
          Called immediately after an element has been put into the cache.
 void notifyElementRemoved(Ehcache cache, Element element)
          Called immediately after an attempt to remove an element.
 void notifyElementUpdated(Ehcache cache, Element element)
          Called immediately after an element has been put into the cache and the element already existed in the cache.
 void notifyRemoveAll(Ehcache cache)
          Called during Ehcache.removeAll() to indicate that the all elements have been removed from the cache in a bulk operation.
protected  void sendNotification(Ehcache cache, JGroupEventMessage e)
          Used to send notification to the peer.
 void setAsynchronousReplicationInterval(long asynchronousReplicationInterval)
          Set the time inteval for asynchronous replication
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ASYNC_INTERVAL

public static final long DEFAULT_ASYNC_INTERVAL
Teh default interval for async cache replication

See Also:
Constant Field Values
Constructor Detail

JGroupsCacheReplicator

public JGroupsCacheReplicator(boolean replicatePuts,
                              boolean replicateUpdates,
                              boolean replicateUpdatesViaCopy,
                              boolean replicateRemovals,
                              boolean replicateAsync)
Constructor called by factory

Parameters:
replicatePuts -
replicateUpdates -
replicateUpdatesViaCopy -
replicateRemovals -
replicateAsync -
Method Detail

isReplicateAsync

public boolean isReplicateAsync()
Weather or not the cache is replicated asynchronously. If true a background thread is ran and fire update at a set intervale

Returns:
true if replicated asynchronously, false otherwise

isReplicatePuts

public boolean isReplicatePuts()
Wether or not puts are replicated

Returns:
true if puts are replicated, false otherwise

isReplicateRemovals

public boolean isReplicateRemovals()
wether or not removals are replicated

Returns:
true if removals are replicated, false otherwise

isReplicateUpdates

public boolean isReplicateUpdates()
Wether or not updates are replicated

Returns:
true if replicated, false otherwise

alive

public boolean alive()
Checks that the replicator is is STATUS_ALIVE.

Specified by:
alive in interface CacheReplicator
Returns:
true if the replicator is is STATUS_ALIVE, else false.

isReplicateUpdatesViaCopy

public boolean isReplicateUpdatesViaCopy()
Returns whether update is through copy or invalidate

Specified by:
isReplicateUpdatesViaCopy in interface CacheReplicator
Returns:
true if update is via copy, else false if invalidate

notAlive

public boolean notAlive()
Returns whether the replicator is not active.

Specified by:
notAlive in interface CacheReplicator
Returns:
true if the status is not STATUS_ALIVE

dispose

public void dispose()
Give the listener a chance to cleanup and free resources when no longer needed

Specified by:
dispose in interface CacheEventListener

notifyElementExpired

public void notifyElementExpired(Ehcache cache,
                                 Element element)
Called immediately after an element is found to be expired. The Cache.remove(Object) method will block until this method returns.

As the Element has been expired, only what was the key of the element is known.

Elements are checked for expiry in ehcache at the following times:

If an element is found to be expired, it is deleted and this method is notified.

Specified by:
notifyElementExpired in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element that has just expired

Deadlock Warning: expiry will often come from the DiskStore expiry thread. It holds a lock to the DiskStorea the time the notification is sent. If the implementation of this method calls into a synchronized Cache method and that subsequently calls into DiskStore a deadlock will result. Accordingly implementers of this method should not call back into Cache.


sendNotification

protected void sendNotification(Ehcache cache,
                                JGroupEventMessage e)
Used to send notification to the peer. If Async this method simply add the element to the replication queue. If not async, searches for the cachePeer and send the Message. That way the class handles both async and sync replication Sending is delegated to the peer (of type JGroupManager)

Parameters:
cache -
e -

notifyElementPut

public void notifyElementPut(Ehcache cache,
                             Element element)
                      throws CacheException
Called immediately after an element has been put into the cache. The Cache.put(net.sf.ehcache.Element) method will block until this method returns.

Implementers may wish to have access to the Element's fields, including value, so the element is provided. Implementers should be careful not to modify the element. The effect of any modifications is undefined.

Specified by:
notifyElementPut in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element which was just put into the cache.
Throws:
CacheException

notifyElementRemoved

public void notifyElementRemoved(Ehcache cache,
                                 Element element)
                          throws CacheException
Called immediately after an attempt to remove an element. The remove method will block until this method returns.

This notification is received regardless of whether the cache had an element matching the removal key or not. If an element was removed, the element is passed to this method, otherwise a synthetic element, with only the key set is passed in.

This notification is not called for the following special cases:

  1. removeAll was called. See CacheEventListener.notifyRemoveAll(net.sf.ehcache.Ehcache)
  2. An element was evicted from the cache. See CacheEventListener.notifyElementEvicted(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)

Specified by:
notifyElementRemoved in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element just deleted, or a synthetic element with just the key set if no element was removed.
Throws:
CacheException

notifyElementUpdated

public void notifyElementUpdated(Ehcache cache,
                                 Element element)
                          throws CacheException
Called immediately after an element has been put into the cache and the element already existed in the cache. This is thus an update.

The Cache.put(net.sf.ehcache.Element) method will block until this method returns.

Implementers may wish to have access to the Element's fields, including value, so the element is provided. Implementers should be careful not to modify the element. The effect of any modifications is undefined.

Specified by:
notifyElementUpdated in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element which was just put into the cache.
Throws:
CacheException

notifyElementEvicted

public void notifyElementEvicted(Ehcache cache,
                                 Element element)
Called immediately after an element is evicted from the cache. Evicted in this sense means evicted from one store and not moved to another, so that it exists nowhere in the local cache.

In a sense the Element has been removed from the cache, but it is different, thus the separate notification.

Specified by:
notifyElementEvicted in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element that has just been evicted

notifyRemoveAll

public void notifyRemoveAll(Ehcache cache)
Called during Ehcache.removeAll() to indicate that the all elements have been removed from the cache in a bulk operation. The usual CacheEventListener.notifyElementRemoved(net.sf.ehcache.Ehcache, net.sf.ehcache.Element) is not called.

This notification exists because clearing a cache is a special case. It is often not practical to serially process notifications where potentially millions of elements have been bulk deleted.

Specified by:
notifyRemoveAll in interface CacheEventListener
Parameters:
cache - the cache emitting the notification

listRemoteCachePeers

static java.util.List listRemoteCachePeers(Ehcache cache)
Package protected List of cache peers

Parameters:
cache -
Returns:
a list of CachePeer peers for the given cache, excluding the local peer.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a clone of this listener. This method will only be called by ehcache before a cache is initialized.

This may not be possible for listeners after they have been initialized. Implementations should throw CloneNotSupportedException if they do not support clone.

Specified by:
clone in interface CacheEventListener
Overrides:
clone in class java.lang.Object
Returns:
a clone
Throws:
java.lang.CloneNotSupportedException - if the listener could not be cloned.

getAsynchronousReplicationInterval

public long getAsynchronousReplicationInterval()
Get the time interval is ms between asynchronous replication

Returns:
the interval

setAsynchronousReplicationInterval

public void setAsynchronousReplicationInterval(long asynchronousReplicationInterval)
Set the time inteval for asynchronous replication

Parameters:
asynchronousReplicationInterval - the interval between replication

ehcache