Class Executable<SdkRequestT,ProtoRequestT extends com.google.protobuf.MessageLite,ResponseT extends com.google.protobuf.MessageLite,O>

java.lang.Object
com.hedera.hashgraph.sdk.Executable<SdkRequestT,ProtoRequestT,ResponseT,O>
Type Parameters:
SdkRequestT - the sdk request
ProtoRequestT - the proto request
ResponseT - the response
O - the O type
Direct Known Subclasses:
Query, Transaction

abstract class Executable<SdkRequestT,ProtoRequestT extends com.google.protobuf.MessageLite,ResponseT extends com.google.protobuf.MessageLite,O> extends Object
Abstract base utility class.
  • Field Details

    • random

      protected static final Random random
    • RST_STREAM

      static final Pattern RST_STREAM
    • maxAttempts

      @Nullable protected Integer maxAttempts
      The maximum times execution will be attempted
    • maxBackoff

      @Nullable protected Duration maxBackoff
      The maximum amount of time to wait between retries
    • minBackoff

      @Nullable protected Duration minBackoff
      The minimum amount of time to wait between retries
    • nodeAccountIds

      protected LockableList<AccountId> nodeAccountIds
      List of account IDs for nodes with which execution will be attempted.
    • nodes

      protected LockableList<Node> nodes
      List of healthy and unhealthy nodes with which execution will be attempted.
    • attemptedAllNodes

      protected boolean attemptedAllNodes
      Indicates if the request has been attempted to be sent to all nodes
    • grpcDeadline

      protected Duration grpcDeadline
      The timeout for each execution attempt
    • logger

      protected Logger logger
    • blockingUnaryCall

      Function<Executable<SdkRequestT,ProtoRequestT extends com.google.protobuf.MessageLite,ResponseT extends com.google.protobuf.MessageLite,O>.GrpcRequest,ResponseT extends com.google.protobuf.MessageLite> blockingUnaryCall
  • Constructor Details

    • Executable

      Executable()
  • Method Details

    • grpcDeadline

      public final Duration grpcDeadline()
      When execution is attempted, a single attempt will time out when this deadline is reached. (The SDK may subsequently retry the execution.)
      Returns:
      The timeout for each execution attempt
    • setGrpcDeadline

      public final SdkRequestT setGrpcDeadline(Duration grpcDeadline)
      When execution is attempted, a single attempt will timeout when this deadline is reached. (The SDK may subsequently retry the execution.)
      Parameters:
      grpcDeadline - The timeout for each execution attempt
      Returns:
      this
    • getMaxBackoff

      public final Duration getMaxBackoff()
      The maximum amount of time to wait between retries
      Returns:
      maxBackoff
    • setMaxBackoff

      public final SdkRequestT setMaxBackoff(Duration maxBackoff)
      The maximum amount of time to wait between retries. Every retry attempt will increase the wait time exponentially until it reaches this time.
      Parameters:
      maxBackoff - The maximum amount of time to wait between retries
      Returns:
      this
    • getMinBackoff

      public final Duration getMinBackoff()
      The minimum amount of time to wait between retries
      Returns:
      minBackoff
    • setMinBackoff

      public final SdkRequestT setMinBackoff(Duration minBackoff)
      The minimum amount of time to wait between retries. When retrying, the delay will start at this time and increase exponentially until it reaches the maxBackoff.
      Parameters:
      minBackoff - The minimum amount of time to wait between retries
      Returns:
      this
    • getMaxRetry

      @Deprecated public final int getMaxRetry()
      Deprecated.
      Use getMaxAttempts() instead.
      Returns:
      Number of errors before execution will fail.
    • setMaxRetry

      @Deprecated public final SdkRequestT setMaxRetry(int count)
      Deprecated.
      Use setMaxAttempts(int) instead.
      Parameters:
      count - Number of errors before execution will fail
      Returns:
      this
    • getMaxAttempts

      public final int getMaxAttempts()
      Get the maximum times execution will be attempted.
      Returns:
      Number of errors before execution will fail.
    • setMaxAttempts

      public final SdkRequestT setMaxAttempts(int maxAttempts)
      Set the maximum times execution will be attempted.
      Parameters:
      maxAttempts - Execution will fail after this many errors.
      Returns:
      this
    • getNodeAccountIds

      @Nullable public final List<AccountId> getNodeAccountIds()
      Get the list of account IDs for nodes with which execution will be attempted.
      Returns:
      the list of account IDs
    • setNodeAccountIds

      public SdkRequestT setNodeAccountIds(List<AccountId> nodeAccountIds)
      Set the account IDs of the nodes that this transaction will be submitted to.

      Providing an explicit node account ID interferes with client-side load balancing of the network. By default, the SDK will pre-generate a transaction for 1/3 of the nodes on the network. If a node is down, busy, or otherwise reports a fatal error, the SDK will try again with a different node.

      Parameters:
      nodeAccountIds - The list of node AccountIds to be set
      Returns:
      this
    • setRequestListener

      public final SdkRequestT setRequestListener(UnaryOperator<ProtoRequestT> requestListener)
      Set a callback that will be called right before the request is sent. As input, the callback will receive the protobuf of the request, and the callback should return the request protobuf. This means the callback has an opportunity to read, copy, or modify the request that will be sent.
      Parameters:
      requestListener - The callback to use
      Returns:
      this
    • setResponseListener

      public final SdkRequestT setResponseListener(UnaryOperator<ResponseT> responseListener)
      Set a callback that will be called right before the response is returned. As input, the callback will receive the protobuf of the response, and the callback should return the response protobuf. This means the callback has an opportunity to read, copy, or modify the response that will be read.
      Parameters:
      responseListener - The callback to use
      Returns:
      this
    • setLogger

      public SdkRequestT setLogger(Logger logger)
      Set the logger
      Parameters:
      logger - the new logger
      Returns:
      this
    • checkNodeAccountIds

      void checkNodeAccountIds()
    • onExecute

      abstract void onExecute(Client client) throws TimeoutException, PrecheckStatusException
      Throws:
      TimeoutException
      PrecheckStatusException
    • onExecuteAsync

      abstract CompletableFuture<Void> onExecuteAsync(Client client)
    • mergeFromClient

      void mergeFromClient(Client client)
    • execute

      public O execute(Client client) throws TimeoutException, PrecheckStatusException
      Execute this transaction or query
      Parameters:
      client - The client with which this will be executed.
      Returns:
      Result of execution
      Throws:
      TimeoutException - when the transaction times out
      PrecheckStatusException - when the precheck fails
    • execute

      public O execute(Client client, Duration timeout) throws TimeoutException, PrecheckStatusException
      Execute this transaction or query with a timeout
      Parameters:
      client - The client with which this will be executed.
      timeout - The timeout after which the execution attempt will be cancelled.
      Returns:
      Result of execution
      Throws:
      TimeoutException - when the transaction times out
      PrecheckStatusException - when the precheck fails
    • isBatchedAndNotBatchTransaction

      protected boolean isBatchedAndNotBatchTransaction()
    • executeAsync

      public CompletableFuture<O> executeAsync(Client client)
      Execute this transaction or query asynchronously.

      Note: This method requires API level 33 or higher. It will not work on devices running API versions below 31 because it uses features introduced in API level 31 (Android 12).

      *
      Parameters:
      client - The client with which this will be executed.
      Returns:
      Future result of execution
    • executeAsync

      public CompletableFuture<O> executeAsync(Client client, Duration timeout)
      Execute this transaction or query asynchronously.

      Note: This method requires API level 33 or higher. It will not work on devices running API versions below 31 because it uses features introduced in API level 31 (Android 12).

      *
      Parameters:
      client - The client with which this will be executed.
      timeout - The timeout after which the execution attempt will be cancelled.
      Returns:
      Future result of execution
    • executeAsync

      public void executeAsync(Client client, BiConsumer<O,Throwable> callback)
      Execute this transaction or query asynchronously.
      Parameters:
      client - The client with which this will be executed.
      callback - a BiConsumer which handles the result or error.
    • executeAsync

      public void executeAsync(Client client, Duration timeout, BiConsumer<O,Throwable> callback)
      Execute this transaction or query asynchronously.
      Parameters:
      client - The client with which this will be executed.
      timeout - The timeout after which the execution attempt will be cancelled.
      callback - a BiConsumer which handles the result or error.
    • executeAsync

      public void executeAsync(Client client, Consumer<O> onSuccess, Consumer<Throwable> onFailure)
      Execute this transaction or query asynchronously.
      Parameters:
      client - The client with which this will be executed.
      onSuccess - a Consumer which consumes the result on success.
      onFailure - a Consumer which consumes the error on failure.
    • executeAsync

      public void executeAsync(Client client, Duration timeout, Consumer<O> onSuccess, Consumer<Throwable> onFailure)
      Execute this transaction or query asynchronously.
      Parameters:
      client - The client with which this will be executed.
      timeout - The timeout after which the execution attempt will be cancelled.
      onSuccess - a Consumer which consumes the result on success.
      onFailure - a Consumer which consumes the error on failure.
    • logTransaction

      protected void logTransaction(TransactionId transactionId, Client client, Node node, boolean isAsync, int attempt, @Nullable ResponseT response, @Nullable Throwable error)
      Logs the transaction's parameters
      Parameters:
      transactionId - the transaction's id
      client - the client that executed the transaction
      node - the node the transaction was sent to
      isAsync - whether the transaction was executed asynchronously
      attempt - the attempt number
      response - the transaction response if the transaction was successful
      error - the error if the transaction was not successful
    • setNodesFromNodeAccountIds

      void setNodesFromNodeAccountIds(Client client)
    • getNodeForExecute

      Node getNodeForExecute(int attempt)
      Return the next node for execution. Will select the first node that is deemed healthy. If we cannot find such a node and have tried n nodes (n being the size of the node list), we will select the node with the smallest remaining delay. All delays MUST be executed in calling layer as this method will be called for sync + async scenarios.
    • makeRequest

      abstract ProtoRequestT makeRequest()
    • getGrpcRequest

    • advanceRequest

      void advanceRequest()
    • mapResponse

      abstract O mapResponse(ResponseT response, AccountId nodeId, ProtoRequestT request)
      Called after receiving the query response from Hedera. The derived class should map into its output type.
    • mapResponseStatus

      abstract Status mapResponseStatus(ResponseT response)
    • getMethodDescriptor

      abstract io.grpc.MethodDescriptor<ProtoRequestT,ResponseT> getMethodDescriptor()
      Called to direct the invocation of the query to the appropriate gRPC service.
    • getTransactionIdInternal

      @Nullable abstract TransactionId getTransactionIdInternal()
    • shouldRetryExceptionally

      boolean shouldRetryExceptionally(@Nullable Throwable error)
    • getExecutionState

      ExecutionState getExecutionState(Status status, ResponseT response)
      Default implementation, may be overridden in subclasses (especially for query case). Called just after receiving the query response from Hedera. By default it triggers a retry when the pre-check status is BUSY.