org.apache.camel.util
Class DefaultTimeoutMap<K,V>

java.lang.Object
  extended by org.apache.camel.impl.ServiceSupport
      extended by org.apache.camel.util.DefaultTimeoutMap<K,V>
All Implemented Interfaces:
Runnable, Service, ShutdownableService, TimeoutMap<K,V>
Direct Known Subclasses:
CorrelationMap

public class DefaultTimeoutMap<K,V>
extends ServiceSupport
implements TimeoutMap<K,V>, Runnable

Default implementation of the TimeoutMap.

This implementation supports thread safe and non thread safe, in the manner you can enable locking or not. By default locking is enabled and thus we are thread safe.

You must provide a ScheduledExecutorService in the constructor which is used to schedule a background task which check for old entries to purge. This implementation will shutdown the scheduler if its being stopped.

Version:

Field Summary
protected  org.slf4j.Logger log
           
 
Constructor Summary
DefaultTimeoutMap(ScheduledExecutorService executor)
           
DefaultTimeoutMap(ScheduledExecutorService executor, long requestMapPollTimeMillis)
           
DefaultTimeoutMap(ScheduledExecutorService executor, long requestMapPollTimeMillis, boolean useLock)
           
 
Method Summary
protected  long currentTime()
           
protected  void doStart()
           
protected  void doStop()
           
 V get(K key)
          Looks up the value in the map by the given key.
 ScheduledExecutorService getExecutor()
           
 Object[] getKeys()
          Returns a copy of the keys in the map
 long getPurgePollTime()
           
protected  boolean isValidForEviction(TimeoutMapEntry<K,V> entry)
          A hook to allow derivations to avoid evicting the current entry
 boolean onEviction(K key, V value)
          Callback when the value has been evicted
 void purge()
          Purges any old entries from the map
 void put(K key, V value, long timeoutMillis)
          Adds the key value pair into the map such that some time after the given timeout the entry will be evicted
 V remove(K id)
          Removes the object with the given key
 void run()
          The timer task which purges old requests and schedules another poll
protected  void schedulePoll()
          lets schedule each time to allow folks to change the time at runtime
 int size()
          Returns the size of the map
protected  void updateExpireTime(TimeoutMapEntry entry)
           
 
Methods inherited from class org.apache.camel.impl.ServiceSupport
addChildService, doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isSuspended, isSuspending, removeChildService, resume, shutdown, start, start, stop, suspend
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final transient org.slf4j.Logger log
Constructor Detail

DefaultTimeoutMap

public DefaultTimeoutMap(ScheduledExecutorService executor)

DefaultTimeoutMap

public DefaultTimeoutMap(ScheduledExecutorService executor,
                         long requestMapPollTimeMillis)

DefaultTimeoutMap

public DefaultTimeoutMap(ScheduledExecutorService executor,
                         long requestMapPollTimeMillis,
                         boolean useLock)
Method Detail

get

public V get(K key)
Description copied from interface: TimeoutMap
Looks up the value in the map by the given key.

Specified by:
get in interface TimeoutMap<K,V>
Parameters:
key - the key of the value to search for
Returns:
the value for the given key or null if it is not present (or has timed out)

put

public void put(K key,
                V value,
                long timeoutMillis)
Description copied from interface: TimeoutMap
Adds the key value pair into the map such that some time after the given timeout the entry will be evicted

Specified by:
put in interface TimeoutMap<K,V>
Parameters:
key - the key
value - the value
timeoutMillis - timeout in millis

remove

public V remove(K id)
Description copied from interface: TimeoutMap
Removes the object with the given key

Specified by:
remove in interface TimeoutMap<K,V>
Parameters:
id - key for the object to remove
Returns:
the value for the given key or null if it is not present (or has timed out)

getKeys

public Object[] getKeys()
Description copied from interface: TimeoutMap
Returns a copy of the keys in the map

Specified by:
getKeys in interface TimeoutMap<K,V>
Returns:
the keys

size

public int size()
Description copied from interface: TimeoutMap
Returns the size of the map

Specified by:
size in interface TimeoutMap<K,V>
Returns:
the size

run

public void run()
The timer task which purges old requests and schedules another poll

Specified by:
run in interface Runnable

purge

public void purge()
Description copied from interface: TimeoutMap
Purges any old entries from the map

Specified by:
purge in interface TimeoutMap<K,V>

getPurgePollTime

public long getPurgePollTime()

getExecutor

public ScheduledExecutorService getExecutor()

schedulePoll

protected void schedulePoll()
lets schedule each time to allow folks to change the time at runtime


isValidForEviction

protected boolean isValidForEviction(TimeoutMapEntry<K,V> entry)
A hook to allow derivations to avoid evicting the current entry


onEviction

public boolean onEviction(K key,
                          V value)
Description copied from interface: TimeoutMap
Callback when the value has been evicted

Specified by:
onEviction in interface TimeoutMap<K,V>
Parameters:
key - the key
value - the value
Returns:
true to remove the evicted value, or false to veto the eviction and thus keep the value.

updateExpireTime

protected void updateExpireTime(TimeoutMapEntry entry)

currentTime

protected long currentTime()

doStart

protected void doStart()
                throws Exception
Specified by:
doStart in class ServiceSupport
Throws:
Exception

doStop

protected void doStop()
               throws Exception
Specified by:
doStop in class ServiceSupport
Throws:
Exception


Apache CAMEL