public class DefaultTimeoutMap<K,V> extends ServiceSupport implements TimeoutMap<K,V>, Runnable
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.
You must also invoke ServiceSupport.start()
to startup the timeout map, before its ready to be used.
And you must invoke ServiceSupport.stop()
to stop the map when no longer in use.Modifier and Type | Field and Description |
---|---|
protected org.slf4j.Logger |
log |
shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
Constructor and Description |
---|
DefaultTimeoutMap(ScheduledExecutorService executor) |
DefaultTimeoutMap(ScheduledExecutorService executor,
long requestMapPollTimeMillis) |
DefaultTimeoutMap(ScheduledExecutorService executor,
long requestMapPollTimeMillis,
boolean useLock) |
Modifier and Type | Method and Description |
---|---|
protected long |
currentTime() |
protected void |
doStart()
Implementations override this method to support customized start/stop.
|
protected void |
doStop()
Implementations override this method to support customized start/stop.
|
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
|
V |
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 |
putIfAbsent(K key,
V value,
long timeoutMillis)
Adds the key value pair into the map if the specified key is not already associated with a value
such that some time after the given timeout the entry will be evicted
|
V |
remove(K key)
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<K,V> entry) |
doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspend
protected final org.slf4j.Logger log
public DefaultTimeoutMap(ScheduledExecutorService executor)
public DefaultTimeoutMap(ScheduledExecutorService executor, long requestMapPollTimeMillis)
public DefaultTimeoutMap(ScheduledExecutorService executor, long requestMapPollTimeMillis, boolean useLock)
public V get(K key)
TimeoutMap
get
in interface TimeoutMap<K,V>
key
- the key of the value to search forpublic V put(K key, V value, long timeoutMillis)
TimeoutMap
put
in interface TimeoutMap<K,V>
key
- the keyvalue
- the valuetimeoutMillis
- timeout in millispublic V putIfAbsent(K key, V value, long timeoutMillis)
TimeoutMap
putIfAbsent
in interface TimeoutMap<K,V>
key
- the keyvalue
- the valuetimeoutMillis
- timeout in millispublic V remove(K key)
TimeoutMap
remove
in interface TimeoutMap<K,V>
key
- key for the object to removepublic Object[] getKeys()
TimeoutMap
getKeys
in interface TimeoutMap<K,V>
public int size()
TimeoutMap
size
in interface TimeoutMap<K,V>
public void run()
public void purge()
TimeoutMap
purge
in interface TimeoutMap<K,V>
public long getPurgePollTime()
public ScheduledExecutorService getExecutor()
protected void schedulePoll()
protected boolean isValidForEviction(TimeoutMapEntry<K,V> entry)
public boolean onEviction(K key, V value)
TimeoutMap
onEviction
in interface TimeoutMap<K,V>
key
- the keyvalue
- the valueprotected void updateExpireTime(TimeoutMapEntry<K,V> entry)
protected long currentTime()
protected void doStart() throws Exception
ServiceSupport
ServiceSupport.doStop()
for more details.doStart
in class ServiceSupport
Exception
ServiceSupport.doStop()
protected void doStop() throws Exception
ServiceSupport
ServiceSupport.doStop()
method when
the service is being stopped. This method will also be invoked
if the service is still in uninitialized state (eg has not
been started). The method is always called to allow the service
to do custom logic when the service is being stopped, such as when
CamelContext
is shutting down.doStop
in class ServiceSupport
Exception
ServiceSupport.doStart()
Apache Camel