Class JdkHttpClient
java.lang.Object
dev.langchain4j.http.client.jdk.JdkHttpClient
- All Implemented Interfaces:
HttpClient, AutoCloseable
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic JdkHttpClientBuilderbuilder()voidclose()Closes the underlying JDK HTTP client on Java 21 and later.execute(HttpRequest request) Executes a given HTTP request synchronously and returns the response.voidexecute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) 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
execute, stream
-
Constructor Details
-
JdkHttpClient
-
-
Method Details
-
builder
-
close
Closes the underlying JDK HTTP client on Java 21 and later. On earlier Java versions, which do not support explicitly closing HTTP clients, this method does nothing.The caller owns this client and should close it when it is no longer needed. Complete or cancel outstanding requests, including streaming subscriptions, before closing it, as closing the JDK HTTP client waits for outstanding requests to finish.
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if the underlying client cannot be closed
-
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
public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) 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.listener- the listener to receive parsed events and error notifications.
-
stream
Description copied from interface:HttpClientLikeHttpClient.stream(HttpRequest), but with a caller-suppliedServerSentEventParser.- Specified by:
streamin interfaceHttpClient
-