Class JdkHttpClientBuilder

java.lang.Object
dev.langchain4j.http.client.jdk.JdkHttpClientBuilder
All Implemented Interfaces:
HttpClientBuilder

public class JdkHttpClientBuilder extends Object implements HttpClientBuilder
  • Constructor Details

    • JdkHttpClientBuilder

      public JdkHttpClientBuilder()
  • Method Details

    • httpClientBuilder

      public HttpClient.Builder httpClientBuilder()
    • httpClientBuilder

      public JdkHttpClientBuilder httpClientBuilder(HttpClient.Builder httpClientBuilder)
      Sets the JDK HttpClient.Builder used to create the underlying client.

      When not set, a default client is used: plain http:// requests are sent over HTTP/1.1, and https:// requests use HTTP/2 when the server supports it.

      When set, the HTTP version configured on the given builder is used for all requests. If no version is configured, the JDK default is HTTP/2, which for plain http:// URLs sends an Upgrade: h2c request that many servers do not handle correctly (for example, they ignore the request body). Call .version(java.net.http.HttpClient.Version.HTTP_1_1) on the builder to avoid this.

      Parameters:
      httpClientBuilder - the JDK HTTP client builder
      Returns:
      this builder
    • connectTimeout

      public Duration connectTimeout()
      Specified by:
      connectTimeout in interface HttpClientBuilder
    • connectTimeout

      public JdkHttpClientBuilder connectTimeout(Duration connectTimeout)
      Specified by:
      connectTimeout in interface HttpClientBuilder
    • readTimeout

      public Duration readTimeout()
      Specified by:
      readTimeout in interface HttpClientBuilder
    • readTimeout

      public JdkHttpClientBuilder readTimeout(Duration readTimeout)
      Specified by:
      readTimeout in interface HttpClientBuilder
    • streamingBufferSize

      @Experimental public Integer streamingBufferSize()
      Returns:
      the bounded back-pressure buffer size used by stream(...), or null for the default.
      Since:
      1.20.0
    • streamingBufferSize

      @Experimental public JdkHttpClientBuilder streamingBufferSize(Integer streamingBufferSize)
      Sets the size of the bounded back-pressure buffer used by the streaming (stream) path. Server-sent events are relayed to the subscriber through this buffer; if the subscriber consumes slower than the server produces and the buffer overflows, the stream terminates with an error.

      The default is 16384. Cannot be combined with streamingBackpressure(true), which does not use this buffer.

      Parameters:
      streamingBufferSize - the buffer size; must be greater than zero
      Returns:
      the builder instance
      Since:
      1.20.0
    • streamingBackpressure

      @Experimental public boolean streamingBackpressure()
      Returns:
      whether streaming HTTP body reads follow downstream demand
      Since:
      1.23.0
    • streamingBackpressure

      @Experimental public JdkHttpClientBuilder streamingBackpressure(boolean streamingBackpressure)
      Controls whether stream(...) propagates subscriber demand to HTTP body reads. When enabled, the client drains the parsed events from the current byte batch before requesting more body data. A slow subscriber pauses body consumption instead of overflowing the event buffer. The parser can still retain an incomplete event spanning several byte batches.

      Disabled by default to retain the bounded event-buffer behavior used by existing integrations. This option applies only to the publisher streaming API, not blocking or callback-based requests. Cannot be combined with streamingBufferSize(Integer): build() throws an IllegalArgumentException if both are set.

      Parameters:
      streamingBackpressure - whether to make streaming body reads follow subscriber demand
      Returns:
      this builder
      Since:
      1.23.0
    • build

      public JdkHttpClient build()
      Specified by:
      build in interface HttpClientBuilder