@ManagedResource(description="File based idempotent repository") public class FileIdempotentRepository extends ServiceSupport implements IdempotentRepository<String>
IdempotentRepository
.
This implementation provides a 1st-level in-memory LRUCache
for fast check of the most
frequently used keys. When add(String)
or contains(String)
methods are being used
then in case of 1st-level cache miss, the underlying file is scanned which may cost additional performance.
So try to find the right balance of the size of the 1st-level cache, the default size is 1000.
The file store has a maximum capacity of 32mb by default (you can turn this off and have unlimited size).
If the file store grows bigger than the maximum capacity, then the getDropOldestFileStore()
(is default 1000)
number of entries from the file store is dropped to reduce the file store and make room for newer entries.shutdown, shuttingdown, started, starting, stopped, stopping, suspended, suspending
Constructor and Description |
---|
FileIdempotentRepository() |
FileIdempotentRepository(File fileStore,
Map<String,Object> set) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(String key)
Adds the key to the repository.
|
protected void |
appendToStore(String key)
Appends the given key to the file store
|
protected void |
cleanup()
Cleanup the 1st-level cache.
|
void |
clear()
Clear the repository.
|
protected void |
clearStore()
Clears the file-store (danger this deletes all entries)
|
boolean |
confirm(String key)
Confirms the key, after the exchange has been processed successfully.
|
boolean |
contains(String key)
Returns true if this repository contains the specified element.
|
protected boolean |
containsStore(String key)
Checks the file store if the key exists
|
protected void |
doStart()
Implementations override this method to support customized start/stop.
|
protected void |
doStop()
Implementations override this method to support customized start/stop.
|
static IdempotentRepository<String> |
fileIdempotentRepository(File fileStore)
Creates a new file based repository using a
LRUCache
as 1st level cache with a default of 1000 entries in the cache. |
static IdempotentRepository<String> |
fileIdempotentRepository(File fileStore,
int cacheSize)
Creates a new file based repository using a
LRUCache
as 1st level cache. |
static IdempotentRepository<String> |
fileIdempotentRepository(File fileStore,
int cacheSize,
long maxFileStoreSize)
Creates a new file based repository using a
LRUCache
as 1st level cache. |
static IdempotentRepository<String> |
fileIdempotentRepository(File store,
Map<String,Object> cache)
Creates a new file based repository using the given
Map
as 1st level cache. |
Map<String,Object> |
getCache() |
int |
getCacheSize() |
long |
getDropOldestFileStore() |
String |
getFilePath() |
File |
getFileStore() |
long |
getMaxFileStoreSize() |
protected void |
loadStore()
Loads the given file store into the 1st level cache
|
boolean |
remove(String key)
Removes the key from the repository.
|
protected void |
removeFromStore(String key) |
void |
reset()
Reset and clears the 1st-level cache to force it to reload from file
|
void |
setCache(Map<String,Object> cache) |
void |
setCacheSize(int size)
Sets the 1st-level cache size.
|
void |
setDropOldestFileStore(long dropOldestFileStore)
Sets the number of oldest entries to drop from the file store when the maximum capacity is hit to reduce
disk space to allow room for new entries.
|
void |
setFileStore(File fileStore) |
void |
setMaxFileStoreSize(long maxFileStoreSize)
Sets the maximum file size for the file store in bytes.
|
protected void |
trunkStore()
Trunks the file store when the max store size is hit by dropping the most oldest entries.
|
doResume, doShutdown, doSuspend, getStatus, getVersion, isRunAllowed, isStarted, isStarting, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspend
public FileIdempotentRepository()
public FileIdempotentRepository(File fileStore, Map<String,Object> set)
public static IdempotentRepository<String> fileIdempotentRepository(File fileStore)
LRUCache
as 1st level cache with a default of 1000 entries in the cache.fileStore
- the file storepublic static IdempotentRepository<String> fileIdempotentRepository(File fileStore, int cacheSize)
LRUCache
as 1st level cache.fileStore
- the file storecacheSize
- the cache sizepublic static IdempotentRepository<String> fileIdempotentRepository(File fileStore, int cacheSize, long maxFileStoreSize)
LRUCache
as 1st level cache.fileStore
- the file storecacheSize
- the cache sizemaxFileStoreSize
- the max size in bytes for the filestore filepublic static IdempotentRepository<String> fileIdempotentRepository(File store, Map<String,Object> cache)
Map
as 1st level cache.
Care should be taken to use a suitable underlying Map
to avoid this class being a
memory leak.store
- the file storecache
- the cache to use as 1st level cache@ManagedOperation(description="Adds the key to the store") public boolean add(String key)
IdempotentRepository
add
in interface IdempotentRepository<String>
key
- the key of the message for duplicate test@ManagedOperation(description="Does the store contain the given key") public boolean contains(String key)
IdempotentRepository
contains
in interface IdempotentRepository<String>
key
- the key of the message@ManagedOperation(description="Remove the key from the store") public boolean remove(String key)
IdempotentRepository
remove
in interface IdempotentRepository<String>
key
- the key of the message for duplicate testpublic boolean confirm(String key)
IdempotentRepository
confirm
in interface IdempotentRepository<String>
key
- the key of the message for duplicate test@ManagedOperation(description="Clear the store (danger this removes all entries)") public void clear()
IdempotentRepository
clear
in interface IdempotentRepository<String>
public File getFileStore()
public void setFileStore(File fileStore)
@ManagedAttribute(description="The file path for the store") public String getFilePath()
@ManagedAttribute(description="The maximum file size for the file store in bytes") public long getMaxFileStoreSize()
@ManagedAttribute(description="The maximum file size for the file store in bytes") public void setMaxFileStoreSize(long maxFileStoreSize)
public long getDropOldestFileStore()
@ManagedAttribute(description="Number of oldest elements to drop from file store if maximum file size reached") public void setDropOldestFileStore(long dropOldestFileStore)
public void setCacheSize(int size)
LRUCache
cache implementation.@ManagedAttribute(description="The current 1st-level cache size") public int getCacheSize()
@ManagedOperation(description="Reset and reloads the file store") public void reset() throws IOException
IOException
protected boolean containsStore(String key)
key
- the keyprotected void appendToStore(String key)
key
- the keyprotected void removeFromStore(String key)
protected void clearStore()
protected void trunkStore()
protected void cleanup()
protected void loadStore() throws IOException
IOException
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