Class HttpClient

java.lang.Object
org.nanonative.nano.core.model.Service
org.nanonative.nano.services.http.HttpClient

public class HttpClient extends Service
  • Field Details

    • CONFIG_HTTP_CLIENT_VERSION

      public static final String CONFIG_HTTP_CLIENT_VERSION
    • CONFIG_HTTP_CLIENT_MAX_RETRIES

      public static final String CONFIG_HTTP_CLIENT_MAX_RETRIES
    • CONFIG_HTTP_CLIENT_CON_TIMEOUT_MS

      public static final String CONFIG_HTTP_CLIENT_CON_TIMEOUT_MS
    • CONFIG_HTTP_CLIENT_READ_TIMEOUT_MS

      public static final String CONFIG_HTTP_CLIENT_READ_TIMEOUT_MS
    • CONFIG_HTTP_CLIENT_FOLLOW_REDIRECTS

      public static final String CONFIG_HTTP_CLIENT_FOLLOW_REDIRECTS
    • EVENT_SEND_HTTP

      public static final int EVENT_SEND_HTTP
    • client

      protected HttpClient client
    • retries

      protected int retries
    • readTimeoutMs

      protected long readTimeoutMs
  • Constructor Details

    • HttpClient

      public HttpClient()
  • Method Details

    • start

      public void start()
      Specified by:
      start in class Service
    • stop

      public void stop()
      Specified by:
      stop in class Service
    • onFailure

      public Object onFailure(Event error)
      Specified by:
      onFailure in class Service
    • onEvent

      public void onEvent(Event event)
      Specified by:
      onEvent in class Service
    • configure

      public void configure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged)
      Specified by:
      configure in class Service
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • send

      public HttpObject send(HttpRequest request)
      Sends an HTTP request using the provided HttpObject or HttpRequest. For async processing, use the send(HttpRequest, Consumer) method.
      Parameters:
      request - the HttpObject or HttpRequest representing the HTTP request to send
      Returns:
      the response as an HttpObject
    • send

      public HttpObject send(HttpRequest request, Consumer<HttpObject> callback)
      Sends an HTTP request using the provided HttpObject or HttpRequest. If a response listener is provided, it processes the response asynchronously.
      Parameters:
      request - the HttpObject or HttpRequest representing the HTTP request to send
      callback - an optional consumer to process the response asynchronously
      Returns:
      the response as an HttpObject
    • retries

      public int retries()
      Returns the number of retries configured for this HttpClient.
      Returns:
      the number of retries
    • followRedirects

      public boolean followRedirects()
      Returns whether this HttpClient follows redirects.
      Returns:
      true if redirects are followed, false otherwise
    • readTimeoutMs

      public long readTimeoutMs()
      Returns the read timeout in milliseconds configured for this HttpClient.
      Returns:
      the read timeout in milliseconds
    • connectionTimeoutMs

      public long connectionTimeoutMs()
      Returns the connection timeout in milliseconds configured for this HttpClient.
      Returns:
      the connection timeout in milliseconds
    • version

      public HttpClient.Version version()
      Returns the HttpClient.Version used by this HttpClient.
      Returns:
      the HttpClient.Version
    • client

      public HttpClient client()
    • send

      protected HttpObject send(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback)
    • responseOf

      protected HttpObject responseOf(HttpResponse<byte[]> httpResponse, HttpObject response)
    • circuitBreaker

      protected HttpObject circuitBreaker(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback, Throwable throwable)
      Implements a circuit breaker pattern to handle retries for HTTP requests in case of failures. This method attempts to resend the request after a delay that increases exponentially with the number of attempts. If the maximum number of retries is reached, it logs the failure and stops retrying.
      Parameters:
      attempt - The current retry attempt number.
      request - The HttpObject representing the original HTTP request.
      response - The HttpObject to populate with the response upon successful request completion.
      throwable - The Throwable that triggered the need for a retry.
      Returns:
      A modified HttpObject containing the result of the retry attempts. If all retries are exhausted without success, it returns the HttpObject populated with the failure information.