T - the type of the resultpublic abstract class AsynchronousAgent<T> extends Object implements FlowControl, Future<T>
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.| Modifier and Type | Field and Description |
|---|---|
protected FlowControl |
m_control
The underlying FlowControl; could be null if the "automatic flow control" is turned on.
|
protected int |
m_iOrderId
A unit-of-order id associated with this agent.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AsynchronousAgent(int iOrderId)
Construct the agent.
|
| Modifier and Type | Method and Description |
|---|---|
void |
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 |
complete(Supplier<T> supplier)
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.
|
T |
get() |
T |
get(long cTimeout, TimeUnit unit) |
CompletableFuture<T> |
getCompletableFuture()
Get the CompletableFuture.
|
Throwable |
getException()
Helper method that returns an exception (if completed exceptionally).
|
T |
getResult()
Helper method that calls
get() and re-throws checked exceptions as a RuntimeException. |
int |
getUnitOfOrderId()
Return a unit-of-order id associated with this agent.
|
boolean |
isCancelled() |
boolean |
isCompletedExceptionally()
Helper method to check if the operation failed.
|
boolean |
isDone() |
protected FlowControl m_control
protected final int m_iOrderId
protected AsynchronousAgent(int iOrderId)
iOrderId - a unit-of-order id associated with this agent. Ordering semantics of operations based on this id are defined by subclassespublic void bind(FlowControl control)
control - the underlying FlowControlpublic void flush()
FlowControlNote: this is a non-blocking call.
flush in interface FlowControlpublic long drainBacklog(long cMillis)
FlowControldrainBacklog in interface FlowControlcMillis - the maximum amount of time to wait (in milliseconds), or zero for infinite waitpublic boolean checkBacklog(Continuation<Void> continueNormal)
FlowControltrue.checkBacklog in interface FlowControlcontinueNormal - (optional) Continuation to be called when the backlog has been reduced back to normalpublic boolean cancel(boolean mayInterruptIfRunning)
public boolean isCancelled()
isCancelled in interface Future<T>public T get() throws InterruptedException, ExecutionException
get in interface Future<T>InterruptedExceptionExecutionExceptionpublic T get(long cTimeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get in interface Future<T>InterruptedExceptionExecutionExceptionTimeoutExceptionpublic int getUnitOfOrderId()
Note 1: the ordering guarantee is respected between AsynchronousAggregators and AsynchronousProcessors with the same unit-of-order id;
Note 2: there is no ordering guarantee between asynchronous and synchronous operations.
AbstractAsynchronousAggregator.getUnitOfOrderId(), AbstractAsynchronousProcessor.getUnitOfOrderId()protected boolean complete(Supplier<T> supplier)
supplier - the supplier of the result of the asynchronous executiontrue if agent could be marked to complete with the given Supplier.protected boolean completeExceptionally(Throwable eReason)
eReason - the reason of failuretrue if agent could be marked to complete with the given exception.public T getResult()
get() and re-throws checked exceptions as a RuntimeException.public Throwable getException()
public boolean isCompletedExceptionally()
public CompletableFuture<T> getCompletableFuture()