Package com.tangosol.util
Class AsynchronousAgent<T>
java.lang.Object
com.tangosol.util.AsynchronousAgent<T>
- Type Parameters:
T
- the type of the result
- All Implemented Interfaces:
FlowControl
,Future<T>
- Direct Known Subclasses:
AbstractAsynchronousAggregator
,AbstractAsynchronousProcessor
Base class for asynchronous operations that provides a simple implementation
of the
Future
interface. It is assumed that subclasses at some point
will either call complete(Supplier)
passing the result supplier
when completed successfully or call completeExceptionally(java.lang.Throwable)
passing the
failure reason.- Author:
- gg/mf 2012.12.21, gg/bb 2015.04.06
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State
-
Field Summary
Modifier and TypeFieldDescriptionprotected FlowControl
The underlying FlowControl; could be null if the "automatic flow control" is turned on.protected final int
A unit-of-order id associated with this agent. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
bind
(FlowControl control) Bind this agent with the specified underlying FlowControl object.boolean
cancel
(boolean mayInterruptIfRunning) boolean
checkBacklog
(Continuation<Void> continueNormal) Check for an excessive backlog and if the underlying communication channel is indeed clogged, call the specified continuation when the backlog is back to normal or the service terminates.protected boolean
Should be called if the operation completed successfully.protected boolean
completeExceptionally
(Throwable eReason) Should be called if the operation failed for any reason.long
drainBacklog
(long cMillis) Check for an excessive backlog and allow blocking the calling thread for up to the specified amount of time.void
flush()
Ensure that any buffered asynchronous operations are dispatched to the underlying tier.get()
Get the CompletableFuture.Helper method that returns an exception (if completed exceptionally).Helper method that callsget()
and re-throws checked exceptions as a RuntimeException.int
Return a unit-of-order id associated with this agent.boolean
boolean
Helper method to check if the operation failed.boolean
isDone()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.Future
exceptionNow, resultNow, state
-
Field Details
-
m_control
The underlying FlowControl; could be null if the "automatic flow control" is turned on. -
m_iOrderId
protected final int m_iOrderIdA unit-of-order id associated with this agent.
-
-
Constructor Details
-
AsynchronousAgent
protected AsynchronousAgent(int iOrderId) Construct the agent.- Parameters:
iOrderId
- a unit-of-order id associated with this agent. Ordering semantics of operations based on this id are defined by subclasses
-
-
Method Details
-
bind
Bind this agent with the specified underlying FlowControl object. This method is to be used only internally by the service.- Parameters:
control
- the underlying FlowControl
-
flush
public void flush()Description copied from interface:FlowControl
Ensure that any buffered asynchronous operations are dispatched to the underlying tier.Note: this is a non-blocking call.
- Specified by:
flush
in interfaceFlowControl
-
drainBacklog
public long drainBacklog(long cMillis) Description copied from interface:FlowControl
Check for an excessive backlog and allow blocking the calling thread for up to the specified amount of time.- Specified by:
drainBacklog
in interfaceFlowControl
- Parameters:
cMillis
- the maximum amount of time to wait (in milliseconds), or zero for infinite wait- Returns:
- the remaining timeout or a negative value if timeout has occurred (the return of zero is only allowed for infinite timeout and indicates that the backlog is no longer excessive)
-
checkBacklog
Description copied from interface:FlowControl
Check for an excessive backlog and if the underlying communication channel is indeed clogged, call the specified continuation when the backlog is back to normal or the service terminates. It's important to remember that:- The continuation could be called on any thread; concurrently with the calling thread or on the calling thread itself.
- The continuation is called if and only if this method returns
true
. - The continuation must not make any blocking calls.
- Specified by:
checkBacklog
in interfaceFlowControl
- Parameters:
continueNormal
- (optional)Continuation
to be called when the backlog has been reduced back to normal- Returns:
- true if the underlying communication channel is backlogged; false otherwise
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public boolean isCancelled()- Specified by:
isCancelled
in interfaceFuture<T>
-
isDone
public boolean isDone() -
get
- Specified by:
get
in interfaceFuture<T>
- Throws:
InterruptedException
ExecutionException
-
get
public T get(long cTimeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
get
in interfaceFuture<T>
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
getUnitOfOrderId
public int getUnitOfOrderId()Return a unit-of-order id associated with this agent. By default, the unit-of-order id is assigned to the calling thread's hashCode.Note 1: the ordering guarantee is respected between
AsynchronousAggregator
s andAsynchronousProcessor
s with the same unit-of-order id;
Note 2: there is no ordering guarantee between asynchronous and synchronous operations.- Returns:
- the order id
- See Also:
-
complete
Should be called if the operation completed successfully.- Parameters:
supplier
- the supplier of the result of the asynchronous execution- Returns:
true
if agent could be marked to complete with the given Supplier.
-
completeExceptionally
Should be called if the operation failed for any reason.- Parameters:
eReason
- the reason of failure- Returns:
true
if agent could be marked to complete with the given exception.
-
getResult
Helper method that callsget()
and re-throws checked exceptions as a RuntimeException.- Returns:
- the result value
-
getException
Helper method that returns an exception (if completed exceptionally).- Returns:
- the exception or null if the operation completed successfully
-
isCompletedExceptionally
public boolean isCompletedExceptionally()Helper method to check if the operation failed.- Returns:
- true if the operation failed
-
getCompletableFuture
Get the CompletableFuture.- Returns:
- CompletableFuture
-