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
    • CONFIG_HTTP_CLIENT_TRUST_ALL

      public static final String CONFIG_HTTP_CLIENT_TRUST_ALL
    • CONFIG_HTTP_CLIENT_TRUSTED_CA

      public static final String CONFIG_HTTP_CLIENT_TRUSTED_CA
    • EVENT_SEND_HTTP

      public static final Channel<HttpObject,HttpObject> 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()
      Description copied from class: Service
      Starts the service. This method is called during service initialization. Optional implementation - services can leave this empty if no startup logic is needed. Common uses include: - Initializing resources - Setting up connections - Starting background tasks See also Service.configure(TypeMapI, TypeMapI) for configuration setup.
      Specified by:
      start in class Service
    • stop

      public void stop()
      Description copied from class: Service
      Stops the service gracefully. This method is called during service shutdown. Optional implementation - services can leave this empty if no cleanup is needed. Common uses include: - Closing connections - Releasing resources - Stopping background tasks
      Specified by:
      stop in class Service
    • onFailure

      public Object onFailure(Event<?,?> error)
      Description copied from class: Service
      Handles service failures and errors. Optional implementation - services can return null if no specific error handling is needed. Null means the error will be logged automatically if no other listener or service handles it. Useful for: - Custom error recovery strategies - Error logging - Notifying other components of failures
      Specified by:
      onFailure in class Service
      Parameters:
      error - The error event to handle
      Returns:
      Response object from error handling, can be null
    • onEvent

      public void onEvent(Event<?,?> event)
      Description copied from class: Service
      Processes incoming events for the service. Optional implementation - services can leave this empty if they don't need to handle events. Useful for: - Responding to system events - Inter-service communication - State updates based on external triggers
      Specified by:
      onEvent in class Service
      Parameters:
      event - The event to process
    • configure

      public void configure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged)
      Description copied from class: Service
      Configures the service with changes while maintaining merged state. Optional implementation - services can leave this empty if no configuration is needed. Useful for: - Service initialization with configuration - Handling dynamic configuration updates - Managing service state - Applying configuration changes without service restart
      Specified by:
      configure in class Service
      Parameters:
      changes - The new configuration changes to apply
      merged - The complete merged configuration state - this will be represented in Service.context after the method is done
    • 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.