Class AbstractStrongCounter
- All Implemented Interfaces:
StrongCounter,CounterEventGenerator,InternalCounterAdmin
- Direct Known Subclasses:
BoundedStrongCounter,UnboundedStrongCounter
Implementation: The value is stored in a single key and it uses the Infinispan's concurrency control and distribution to apply the write and reads. It uses the functional API.
Writes: The writes are performed by the functional API in order. The single key approach allows us to provide atomic properties for the counter value.
Reads: The reads read the value from the cache and it can go remotely.
Weak Reads: This implementation supports weak cached reads. It uses clustered listeners to receive the notifications of the actual value to store it locally.
- Since:
- 9.0
- Author:
- Pedro Ruivo
-
Method Summary
Modifier and TypeMethodDescriptionfinal CompletableFuture<Long>addAndGet(long delta) Atomically adds the given value and return the new value.final <T extends CounterListener>
Handle<T>addListener(T listener) Registers aCounterListenerto this counter.compareAndSwap(long expect, long update) Atomically sets the value to the given updated value if the current value==the expected value.destroy()Destroys the counter.generate(CounterKey key, CounterValue value) It generates theCounterEvent.getAndSet(long value) Atomically sets the value to the given updated valuefinal CounterConfigurationfinal StringgetName()final CompletableFuture<Long>getValue()It fetches the current value.protected abstract longhandleAddResult(CounterValue counterValue) Extracts and validates the value after a read.protected abstract LonghandleCASResult(Object state) protected abstract LonghandleSetResult(Object state) init()booleanChecks if the counter is aWeakCounter.remove()It removes this counter from the cluster.static CompletionStage<Void>removeStrongCounter(org.infinispan.Cache<StrongCounterKey, CounterValue> cache, String counterName) It removes a strong counter from thecache, identified by thecounterName.final CompletableFuture<Void>reset()Resets the counter to its initial value.sync()It returns a synchronous strong counter for this instance.value()Returns the counter's value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.infinispan.counter.impl.manager.InternalCounterAdmin
asWeakCounterMethods inherited from interface org.infinispan.counter.api.StrongCounter
compareAndSet, decrementAndGet, incrementAndGet
-
Method Details
-
removeStrongCounter
public static CompletionStage<Void> removeStrongCounter(org.infinispan.Cache<StrongCounterKey, CounterValue> cache, String counterName) It removes a strong counter from thecache, identified by thecounterName.- Parameters:
cache- TheCacheto remove the counter from.counterName- The counter's name.
-
init
-
asStrongCounter
- Specified by:
asStrongCounterin interfaceInternalCounterAdmin- Returns:
- The
StrongCounterinstance or throws anCounterExceptionif the counter is not aStrongCounter.
-
destroy
Description copied from interface:InternalCounterAdminDestroys the counter.It drops the counter's value and all listeners registered.
- Specified by:
destroyin interfaceInternalCounterAdmin- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
value
Description copied from interface:InternalCounterAdminReturns the counter's value.- Specified by:
valuein interfaceInternalCounterAdmin- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
isWeakCounter
public boolean isWeakCounter()Description copied from interface:InternalCounterAdminChecks if the counter is aWeakCounter.If
true, ensuresInternalCounterAdmin.asWeakCounter()never throws anCounterException. Otherwise, it ensuresInternalCounterAdmin.asStrongCounter()never throws anCounterException.- Specified by:
isWeakCounterin interfaceInternalCounterAdmin- Returns:
trueif the counter isWeakCounter.
-
getName
- Specified by:
getNamein interfaceStrongCounter- Returns:
- The counter name.
-
getValue
Description copied from interface:StrongCounterIt fetches the current value.It may go remotely to fetch the current value.
- Specified by:
getValuein interfaceStrongCounter- Returns:
- The current value.
-
addAndGet
Description copied from interface:StrongCounterAtomically adds the given value and return the new value.- Specified by:
addAndGetin interfaceStrongCounter- Parameters:
delta- The non-zero value to add. It can be negative.- Returns:
- The new value.
-
reset
Description copied from interface:StrongCounterResets the counter to its initial value.- Specified by:
resetin interfaceInternalCounterAdmin- Specified by:
resetin interfaceStrongCounter- Returns:
- A
CompletionStageinstance which is completed when the operation finishes.
-
addListener
Description copied from interface:StrongCounterRegisters aCounterListenerto this counter.- Specified by:
addListenerin interfaceStrongCounter- Type Parameters:
T- The concrete type of the listener. It must implementCounterListener.- Parameters:
listener- The listener to register.- Returns:
- A
Handlethat allows to remove the listener viaHandle.remove().
-
compareAndSwap
Description copied from interface:StrongCounterAtomically sets the value to the given updated value if the current value==the expected value.The operation is successful if the return value is equals to the expected value.
- Specified by:
compareAndSwapin interfaceStrongCounter- Parameters:
expect- the expected value.update- the new value.- Returns:
- the previous counter's value.
-
getAndSet
Description copied from interface:StrongCounterAtomically sets the value to the given updated value- Specified by:
getAndSetin interfaceStrongCounter- Parameters:
value- the expected value.- Returns:
- the previous counter's value.
-
generate
Description copied from interface:CounterEventGeneratorIt generates theCounterEvent.The
valueis the new value ofCounterEvent.- Specified by:
generatein interfaceCounterEventGenerator- Parameters:
key- The counter's key.value- The counter's most recentCounterValue.- Returns:
- The
CounterEventwith the updated value.
-
remove
Description copied from interface:StrongCounterIt removes this counter from the cluster.Note that it doesn't remove the counter from the
CounterManager. If you want to remove the counter from theCounterManageruseCounterManager.remove(String).- Specified by:
removein interfaceStrongCounter- Returns:
- The
CompletableFuturethat is completed when the counter is removed from the cluster.
-
getConfiguration
- Specified by:
getConfigurationin interfaceStrongCounter- Returns:
- the
CounterConfigurationused by this counter.
-
sync
Description copied from interface:StrongCounterIt returns a synchronous strong counter for this instance.- Specified by:
syncin interfaceStrongCounter- Returns:
- a
SyncStrongCounter.
-
handleCASResult
-
handleAddResult
Extracts and validates the value after a read.Any exception should be thrown using
CompletionException.- Parameters:
counterValue- The newCounterValue.- Returns:
- The new value stored in
CounterValue.
-
handleSetResult
-