Class HttpClient
java.lang.Object
org.nanonative.nano.core.model.Service
org.nanonative.nano.services.http.HttpClient
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected HttpClientstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Channel<HttpObject, HttpObject> protected longprotected intFields inherited from class org.nanonative.nano.core.model.Service
context, createdAtNs, isReady -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected HttpObjectcircuitBreaker(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.client()voidconfigure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged) Configures the service with changes while maintaining merged state.longReturns the connection timeout in milliseconds configured for thisHttpClient.booleanReturns whether thisHttpClientfollows redirects.voidProcesses incoming events for the service.Handles service failures and errors.longReturns the read timeout in milliseconds configured for thisHttpClient.protected HttpObjectresponseOf(HttpResponse<byte[]> httpResponse, HttpObject response) intretries()Returns the number of retries configured for thisHttpClient.protected HttpObjectsend(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback) send(HttpRequest request) Sends an HTTP request using the providedHttpObjectorHttpRequest.send(HttpRequest request, Consumer<HttpObject> callback) Sends an HTTP request using the providedHttpObjectorHttpRequest.voidstart()Starts the service.voidstop()Stops the service gracefully.toString()version()Returns theHttpClient.Versionused by thisHttpClient.Methods inherited from class org.nanonative.nano.core.model.Service
configure, context, context, createdAtNs, isReady, isReadyState, name, nanoThread, receiveEvent, threadsOf
-
Field Details
-
CONFIG_HTTP_CLIENT_VERSION
-
CONFIG_HTTP_CLIENT_MAX_RETRIES
-
CONFIG_HTTP_CLIENT_CON_TIMEOUT_MS
-
CONFIG_HTTP_CLIENT_READ_TIMEOUT_MS
-
CONFIG_HTTP_CLIENT_FOLLOW_REDIRECTS
-
CONFIG_HTTP_CLIENT_TRUST_ALL
-
CONFIG_HTTP_CLIENT_TRUSTED_CA
-
EVENT_SEND_HTTP
-
client
-
retries
protected int retries -
readTimeoutMs
protected long readTimeoutMs
-
-
Constructor Details
-
HttpClient
public HttpClient()
-
-
Method Details
-
start
public void start()Description copied from class:ServiceStarts 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 alsoService.configure(TypeMapI, TypeMapI)for configuration setup. -
stop
public void stop()Description copied from class:ServiceStops 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 -
onFailure
Description copied from class:ServiceHandles 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 -
onEvent
Description copied from class:ServiceProcesses 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 -
configure
public void configure(berlin.yuna.typemap.model.TypeMapI<?> changes, berlin.yuna.typemap.model.TypeMapI<?> merged) Description copied from class:ServiceConfigures 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:
configurein classService- Parameters:
changes- The new configuration changes to applymerged- The complete merged configuration state - this will be represented inService.contextafter the method is done
-
toString
-
send
Sends an HTTP request using the providedHttpObjectorHttpRequest. For async processing, use thesend(HttpRequest, Consumer)method.- Parameters:
request- theHttpObjectorHttpRequestrepresenting the HTTP request to send- Returns:
- the response as an
HttpObject
-
send
Sends an HTTP request using the providedHttpObjectorHttpRequest. If a response listener is provided, it processes the response asynchronously.- Parameters:
request- theHttpObjectorHttpRequestrepresenting the HTTP request to sendcallback- 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 thisHttpClient.- Returns:
- the number of retries
-
followRedirects
public boolean followRedirects()Returns whether thisHttpClientfollows redirects.- Returns:
trueif redirects are followed,falseotherwise
-
readTimeoutMs
public long readTimeoutMs()Returns the read timeout in milliseconds configured for thisHttpClient.- Returns:
- the read timeout in milliseconds
-
connectionTimeoutMs
public long connectionTimeoutMs()Returns the connection timeout in milliseconds configured for thisHttpClient.- Returns:
- the connection timeout in milliseconds
-
version
Returns theHttpClient.Versionused by thisHttpClient.- Returns:
- the
HttpClient.Version
-
client
-
send
protected HttpObject send(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback) -
responseOf
-
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- TheHttpObjectrepresenting the original HTTP request.response- TheHttpObjectto populate with the response upon successful request completion.throwable- TheThrowablethat triggered the need for a retry.- Returns:
- A modified
HttpObjectcontaining the result of the retry attempts. If all retries are exhausted without success, it returns theHttpObjectpopulated with the failure information.
-