Class AsynchronousAgent<T>

    • Field Detail

      • m_control

        protected FlowControl m_control
        The underlying FlowControl; could be null if the "automatic flow control" is turned on.
      • m_iOrderId

        protected final int m_iOrderId
        A unit-of-order id associated with this agent.
    • Constructor Detail

      • 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

        protected AsynchronousAgent​(int iOrderId,
                                    Executor executor)
        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
        executor - an optional Executor to complete the future on, if not provided the Daemons.commonPool() is used
    • Method Detail

      • bind

        public void bind​(FlowControl control)
        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 interface FlowControl
      • 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 interface FlowControl
        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

        public boolean checkBacklog​(Continuation<Void> continueNormal)
        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:
        1. The continuation could be called on any thread; concurrently with the calling thread or on the calling thread itself.
        2. The continuation is called if and only if this method returns true.
        3. The continuation must not make any blocking calls.
        Specified by:
        checkBacklog in interface FlowControl
        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)
        Specified by:
        cancel in interface Future<T>
      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface Future<T>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface Future<T>
      • complete

        protected boolean complete​(Supplier<T> supplier)
        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

        protected boolean completeExceptionally​(Throwable eReason)
        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

        public T getResult()
        Helper method that calls get() and re-throws checked exceptions as a RuntimeException.
        Returns:
        the result value
      • getException

        public Throwable 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

        public CompletableFuture<T> getCompletableFuture()
        Get the CompletableFuture.
        Returns:
        CompletableFuture