Class JdkHttpClient

java.lang.Object
dev.langchain4j.http.client.jdk.JdkHttpClient
All Implemented Interfaces:
HttpClient, AutoCloseable

public class JdkHttpClient extends Object implements HttpClient, AutoCloseable
  • Constructor Details

  • Method Details

    • builder

      public static JdkHttpClientBuilder builder()
    • close

      public void close() throws IOException
      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:
      close in interface AutoCloseable
      Throws:
      IOException - if the underlying client cannot be closed
    • execute

      public SuccessfulHttpResponse execute(HttpRequest request) throws HttpException
      Description copied from interface: HttpClient
      Executes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.
      Specified by:
      execute in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a SuccessfulHttpResponse containing 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

      public CompletableFuture<SuccessfulHttpResponse> executeAsync(HttpRequest request)
      Description copied from interface: HttpClient
      Non-blocking counterpart of HttpClient.execute(HttpRequest). Returns immediately with a CompletableFuture that completes with the SuccessfulHttpResponse once the full response has been received, without blocking the calling thread. The future completes exceptionally with an HttpException for non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.

      An implementation must not throw and must not return null: every failure, including an invalid request (for example an invalid URL or header), completes the returned future exceptionally.

      Specified by:
      executeAsync in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a CompletableFuture of the SuccessfulHttpResponse.
    • execute

      public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener)
      Description copied from interface: HttpClient
      Executes 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 provided ServerSentEventListener.

      The execution flow is as follows:

      1. The request is initiated asynchronously
      2. Received SSE data is parsed using the provided parser
      3. Parsed events are delivered to the listener's appropriate methods
      4. 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.

      An implementation does not throw: every failure, including an invalid request (for example an invalid URL or header), is passed to ServerSentEventListener.onError(Throwable), possibly before this method returns. If onError throws while it is called on the calling thread, that exception is thrown from this method.

      Specified by:
      execute in interface HttpClient
      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: HttpClient
      Like HttpClient.stream(HttpRequest), but with a caller-supplied ServerSentEventParser.

      An implementation must not throw and must not return null: every failure, including an invalid request, reaches the subscriber's onError, after onSubscribe.

      Specified by:
      stream in interface HttpClient