Class InMemoryIdempotencyStore
- All Implemented Interfaces:
IdempotencyStore
Honest about what it is: this deduplicates within one process and forgets everything when that process restarts. That is enough for a single consumer whose duplicates arrive seconds apart, which is the common case, and it is not enough for a consumer group spread across several instances, where a duplicate can land on a different machine from the original. For that the store has to be shared — Redis or a database table — and this class is the shape such an implementation takes.
Both bounds exist to stop the obvious failure. Unbounded, this is a memory leak with a business purpose: every message identifier ever seen, retained forever. Entries expire after a retention period, and the map is capped, because a store that runs a service out of memory has done more damage than the duplicates it prevented.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.acemq.amqp.api.IdempotencyStore
IdempotencyStore.ClaimResult -
Constructor Summary
ConstructorsConstructorDescriptionInMemoryIdempotencyStore(Duration retention) InMemoryIdempotencyStore(Duration retention, int maxEntries) InMemoryIdempotencyStore(Duration retention, int maxEntries, Duration claimTimeout) -
Method Summary
Modifier and TypeMethodDescriptionbooleanTakes ownership of a message identifier, if nobody else has it.voidRecords that the work for a claimed identifier completed.longstatic InMemoryIdempotencyStorebooleanisConfirmed(String messageId) voidGives up a claim without recording completion, so the message can be tried again.intsize()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.acemq.amqp.api.IdempotencyStore
tryClaim
-
Constructor Details
-
InMemoryIdempotencyStore
- Parameters:
retention- how long a confirmed identifier is remembered; duplicates arriving later than this are handled again
-
InMemoryIdempotencyStore
- Parameters:
retention- how long a confirmed identifier is rememberedmaxEntries- hard cap on remembered identifiers
-
InMemoryIdempotencyStore
- Parameters:
retention- how long a confirmed identifier is rememberedmaxEntries- hard cap on remembered identifiersclaimTimeout- how long a claim that was never confirmed or released holds the identifier before another delivery may take it over. While it holds, redeliveries are sent back to the queue as in progress rather than handled or acknowledged
-
-
Method Details
-
forOneDay
- Returns:
- a store that remembers identifiers for a day, a reasonable default when the alternative is a duplicate charge or a duplicate email
-
claim
Description copied from interface:IdempotencyStoreTakes ownership of a message identifier, if nobody else has it.- Specified by:
claimin interfaceIdempotencyStore- Parameters:
messageId- the identifier, normallyEnvelope.id()- Returns:
truewhen the caller now owns this identifier and should do the work;falsewhen it is already confirmed or claimed elsewhere. Which of the two it is matters — only a confirmed one is a duplicate — so consumers callIdempotencyStore.tryClaim(java.lang.String)
-
confirm
Description copied from interface:IdempotencyStoreRecords that the work for a claimed identifier completed.After this,
IdempotencyStore.claim(java.lang.String)returnsfalsefor the identifier until the store forgets it, which is what stops a redelivery from repeating the work.- Specified by:
confirmin interfaceIdempotencyStore- Parameters:
messageId- the identifier being confirmed
-
release
Description copied from interface:IdempotencyStoreGives up a claim without recording completion, so the message can be tried again.Called when a handler fails. Without it a failed attempt would poison the identifier and the retry would be discarded as a duplicate, which turns a transient failure into permanent message loss.
- Specified by:
releasein interfaceIdempotencyStore- Parameters:
messageId- the identifier being released
-
isConfirmed
- Specified by:
isConfirmedin interfaceIdempotencyStore- Parameters:
messageId- the identifier to test- Returns:
- whether the work for this identifier is already known to have completed
-
size
public int size()- Returns:
- how many identifiers are currently remembered
-
evictions
public long evictions()- Returns:
- how many identifiers have been dropped to stay within the size cap
-