net.sf.ehcache.distribution

Class RMIAsynchronousCacheReplicator

public final class RMIAsynchronousCacheReplicator extends RMISynchronousCacheReplicator

Listens to CacheManager and Cache events and propagates those to CachePeer peers of the Cache asynchronously.

Updates are guaranteed to be replicated in the order in which they are received.

While much faster in operation than RMISynchronousCacheReplicator, it does suffer from a number of problems. Elements, which may be being spooled to DiskStore may stay around in memory because references are being held to them from EventMessages which are queued up. The replication thread runs once per second, limiting the build up. However a lot of elements can be put into a cache in that time. We do not want to get an OutOfMemoryError using distribution in circumstances when it would not happen if we were just using the DiskStore.

Accordingly, EventMessages are held by SoftReference in the queue, so that they can be discarded if required by the GC to avoid an OutOfMemoryError. A log message will be issued on each flush of the queue if there were any forced discards. One problem with GC collection of SoftReferences is that the VM (JDK1.5 anyway) will do that rather than grow the heap size to the maximum. The workaround is to either set minimum heap size to the maximum heap size to force heap allocation at start up, or put up with a few lost messages while the heap grows.

Version: $Id: RMIAsynchronousCacheReplicator.java 141 2006-06-30 01:56:32Z gregluck $

Author: Greg Luck

Field Summary
protected ListreplicationQueue
A queue of updates.
protected ThreadreplicationThread
A thread which handles replication, so that replication can take place asynchronously and not hold up the cache
protected static intREPLICATION_THREAD_INTERVAL
The amount of time the replication thread sleeps after it detects the replicationQueue is empty before checking again.
Constructor Summary
protected RMIAsynchronousCacheReplicator(boolean replicatePuts, boolean replicateUpdates, boolean replicateUpdatesViaCopy, boolean replicateRemovals)
Constructor for internal and subclass use
Method Summary
protected voidaddToReplicationQueue(RMIAsynchronousCacheReplicator.CacheEventMessage cacheEventMessage)
Adds a message to the queue.
voiddispose()
Give the replicator a chance to cleanup and free resources when no longer needed
voidnotifyElementPut(Cache cache, Element element)
{@inheritDoc }

This implementation queues the put notification for in-order replication to peers.

voidnotifyElementRemoved(Cache cache, Element element)
Called immediately after an element has been removed.
voidnotifyElementUpdated(Cache cache, Element element)
Called immediately after an element has been put into the cache and the element already existed in the cache.

Field Detail

replicationQueue

protected final List replicationQueue
A queue of updates.

replicationThread

protected final Thread replicationThread
A thread which handles replication, so that replication can take place asynchronously and not hold up the cache

REPLICATION_THREAD_INTERVAL

protected static final int REPLICATION_THREAD_INTERVAL
The amount of time the replication thread sleeps after it detects the replicationQueue is empty before checking again.

Constructor Detail

RMIAsynchronousCacheReplicator

protected RMIAsynchronousCacheReplicator(boolean replicatePuts, boolean replicateUpdates, boolean replicateUpdatesViaCopy, boolean replicateRemovals)
Constructor for internal and subclass use

Parameters: replicatePuts replicateUpdates replicateUpdatesViaCopy replicateRemovals

Method Detail

addToReplicationQueue

protected void addToReplicationQueue(RMIAsynchronousCacheReplicator.CacheEventMessage cacheEventMessage)
Adds a message to the queue.

This method checks the state of the replication thread and warns if it has stopped and then discards the message.

Parameters: cacheEventMessage

dispose

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

notifyElementPut

public final void notifyElementPut(Cache cache, Element element)
{@inheritDoc }

This implementation queues the put notification for in-order replication to peers.

Parameters: cache the cache emitting the notification element the element which was just put into the cache.

notifyElementRemoved

public final void notifyElementRemoved(Cache cache, Element element)
Called immediately after an element has been removed. The remove method will block until this method returns.

This implementation queues the removal notification for in order replication to peers.

Parameters: cache the cache emitting the notification element just deleted

notifyElementUpdated

public final void notifyElementUpdated(Cache cache, Element element)
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 put 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.

Parameters: cache the cache emitting the notification element the element which was just put into the cache.