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
FieldsModifier and TypeFieldDescriptionprotected FlowControlThe underlying FlowControl; could be null if the "automatic flow control" is turned on.Converter from internal format.protected final intA unit-of-order id associated with this agent. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAsynchronousAgent(int iOrderId) Construct the agent.protectedAsynchronousAgent(int iOrderId, Executor executor) Construct the agent. -
Method Summary
Modifier and TypeMethodDescriptionvoidbind(FlowControl control) Bind this agent with the specified underlying FlowControl object.booleancancel(boolean mayInterruptIfRunning) booleancheckBacklog(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 booleanShould be called if the operation completed successfully.protected booleancompleteExceptionally(Throwable eReason) Should be called if the operation failed for any reason.longdrainBacklog(long cMillis) Check for an excessive backlog and allow blocking the calling thread for up to the specified amount of time.voidflush()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.intReturn a unit-of-order id associated with this agent.booleanbooleanHelper method to check if the operation failed.booleanisDone()voidsetValueConverter(Converter<Binary, T> converter) Set the converter from internal format.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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. -
m_converter
Converter from internal format.
-
-
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
-
AsynchronousAgent
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 subclassesexecutor- an optionalExecutorto complete the future on, if not provided theDaemons.commonPool()is used
-
-
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:FlowControlEnsure that any buffered asynchronous operations are dispatched to the underlying tier.Note: this is a non-blocking call.
- Specified by:
flushin interfaceFlowControl
-
drainBacklog
public long drainBacklog(long cMillis) Description copied from interface:FlowControlCheck for an excessive backlog and allow blocking the calling thread for up to the specified amount of time.- Specified by:
drainBacklogin 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:FlowControlCheck 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:
checkBacklogin interfaceFlowControl- Parameters:
continueNormal- (optional)Continuationto 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:
isCancelledin interfaceFuture<T>
-
isDone
public boolean isDone() -
get
- Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionException
-
get
public T get(long cTimeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
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
AsynchronousAggregators andAsynchronousProcessors 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:
trueif 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:
trueif 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
-
setValueConverter
Set the converter from internal format.- Parameters:
converter- the converter from internal format- Implementation Note:
- This method is only called by RequestCoordinator.createContext, so it is very much internal, but it has to be public in order to be accessible.
-