Class LoggingHttpClient
- All Implemented Interfaces:
HttpClient
HttpClient decorator that logs requests and responses.
Streaming response logging is blocking IO on the delivery thread. For the streaming paths
(execute(HttpRequest, ServerSentEventListener),
execute(HttpRequest, ServerSentEventParser, ServerSentEventListener) and especially
stream(HttpRequest, ServerSentEventParser)), each server-sent event is logged
synchronously on the thread that delivers it — for the publisher path this is the underlying HTTP
client's non-blocking worker thread. Whether that log call actually blocks is decided by the logging
backend's appender, which this client does not control: a synchronous appender (e.g. a plain
file/console appender) performs a blocking write on the worker thread, which under load can stall the
worker and collapse streaming throughput. Therefore, when logResponses is enabled for
streaming, configure an asynchronous appender (e.g. Logback AsyncAppender, Log4j2 async
loggers, tinylog writingthread=true) so log writes happen off the delivery thread.
-
Constructor Summary
ConstructorsConstructorDescriptionLoggingHttpClient(HttpClient delegateHttpClient, Boolean logRequests, Boolean logResponses) LoggingHttpClient(HttpClient delegateHttpClient, Boolean logRequests, Boolean logResponses, org.slf4j.Logger logger) -
Method Summary
Modifier and TypeMethodDescriptionexecute(HttpRequest request) Executes a given HTTP request synchronously and returns the response.voidexecute(HttpRequest request, ServerSentEventListener delegateListener) Executes a given HTTP request asynchronously with server-sent events (SSE) handling.voidexecute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener delegateListener) Executes a given HTTP request asynchronously with server-sent events (SSE) handling.executeAsync(HttpRequest request) Non-blocking counterpart ofHttpClient.execute(HttpRequest).stream(HttpRequest request, ServerSentEventParser parser) LikeHttpClient.stream(HttpRequest), but with a caller-suppliedServerSentEventParser.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpClient
stream
-
Constructor Details
-
LoggingHttpClient
-
LoggingHttpClient
public LoggingHttpClient(HttpClient delegateHttpClient, Boolean logRequests, Boolean logResponses, org.slf4j.Logger logger)
-
-
Method Details
-
execute
Description copied from interface:HttpClientExecutes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.- Specified by:
executein interfaceHttpClient- Parameters:
request- the HTTP request to be executed.- Returns:
- a
SuccessfulHttpResponsecontaining the response data for successful HTTP requests (2XX status codes) - Throws:
HttpException- if the server returns a client (4XX) or server (5XX) error response
-
executeAsync
Description copied from interface:HttpClientNon-blocking counterpart ofHttpClient.execute(HttpRequest). Returns immediately with aCompletableFuturethat completes with theSuccessfulHttpResponseonce the full response has been received, without blocking the calling thread. The future completes exceptionally with anHttpExceptionfor non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.- Specified by:
executeAsyncin interfaceHttpClient- Parameters:
request- the HTTP request to be executed.- Returns:
- a
CompletableFutureof theSuccessfulHttpResponse.
-
execute
Description copied from interface:HttpClientExecutes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the providedServerSentEventListener.The execution flow is as follows:
- The request is initiated asynchronously
- Received SSE data is parsed using the
DefaultServerSentEventParser - Parsed events are delivered to the listener's appropriate methods
- If an error occurs,
ServerSentEventListener.onError(Throwable)is called
If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.
- Specified by:
executein interfaceHttpClient- Parameters:
request- the HTTP request to be executed.delegateListener- the listener to receive parsed events and error notifications.
-
execute
public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener delegateListener) Description copied from interface:HttpClientExecutes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the providedServerSentEventListener.The execution flow is as follows:
- The request is initiated asynchronously
- Received SSE data is parsed using the provided parser
- Parsed events are delivered to the listener's appropriate methods
- If an error occurs,
ServerSentEventListener.onError(Throwable)is called
If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.
- Specified by:
executein interfaceHttpClient- Parameters:
request- the HTTP request to be executed.parser- the parser to process incoming server-sent events.delegateListener- the listener to receive parsed events and error notifications.
-
stream
LikeHttpClient.stream(HttpRequest), but with a caller-suppliedServerSentEventParser.When
logResponsesis enabled, each event is logged inonNext, i.e. synchronously on the upstream's delivery (worker) thread. See the class-level note: use an asynchronous appender so this logging does not perform blocking IO on that non-blocking thread.- Specified by:
streamin interfaceHttpClient
-