Class JdkHttpClientBuilder

java.lang.Object
com.azure.core.http.jdk.httpclient.JdkHttpClientBuilder

public class JdkHttpClientBuilder extends Object
Builder to configure and build an instance of the azure-core HttpClient type using the JDK HttpClient APIs, first introduced as preview in JDK 9, but made generally available from JDK 11 onwards.
  • Constructor Details

    • JdkHttpClientBuilder

      public JdkHttpClientBuilder()
      Creates JdkHttpClientBuilder.
    • JdkHttpClientBuilder

      public JdkHttpClientBuilder(java.net.http.HttpClient.Builder httpClientBuilder)
      Creates JdkHttpClientBuilder from the builder of an existing HttpClient.Builder.
      Parameters:
      httpClientBuilder - the HttpClient builder to use
      Throws:
      NullPointerException - if httpClientBuilder is null
  • Method Details

    • executor

      public JdkHttpClientBuilder executor(Executor executor)
      Sets the executor to be used for asynchronous and dependent tasks. This cannot be null.

      If this method is not invoked prior to building, a default executor is created for each newly built HttpClient.

      Parameters:
      executor - the executor to be used for asynchronous and dependent tasks
      Returns:
      the updated JdkHttpClientBuilder object
      Throws:
      NullPointerException - if executor is null
    • connectionTimeout

      public JdkHttpClientBuilder connectionTimeout(Duration connectionTimeout)
      Sets the connection timeout.

      Code Samples

       HttpClient client = new JdkHttpClientBuilder()
               .connectionTimeout(Duration.ofSeconds(250)) // connection timeout of 250 seconds
               .build();
       
      The default connection timeout is 10 seconds.
      Parameters:
      connectionTimeout - the connection timeout
      Returns:
      the updated JdkHttpClientBuilder object
    • writeTimeout

      public JdkHttpClientBuilder writeTimeout(Duration writeTimeout)
      Sets the writing timeout for a request to be sent.

      The writing timeout does not apply to the entire request but to the request being sent over the wire. For example a request body which emits 10 8KB buffers will trigger 10 write operations, the last write tracker will update when each operation completes and the outbound buffer will be periodically checked to determine if it is still draining.

      If writeTimeout is null either Configuration.PROPERTY_AZURE_REQUEST_WRITE_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no write timeout will be applied. When applying the timeout the greatest of one millisecond and the value of writeTimeout will be used.

      Parameters:
      writeTimeout - Write operation timeout duration.
      Returns:
      The updated JdkHttpClientBuilder object.
    • responseTimeout

      public JdkHttpClientBuilder responseTimeout(Duration responseTimeout)
      Sets the response timeout duration used when waiting for a server to reply.

      The response timeout begins once the request write completes and finishes once the first response read is triggered when the server response is received.

      If responseTimeout is null either Configuration.PROPERTY_AZURE_REQUEST_RESPONSE_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no timeout will be applied to the response. When applying the timeout the greatest of one millisecond and the value of responseTimeout will be used.

      Parameters:
      responseTimeout - Response timeout duration.
      Returns:
      The updated JdkHttpClientBuilder object.
    • readTimeout

      public JdkHttpClientBuilder readTimeout(Duration readTimeout)
      Sets the read timeout duration used when reading the server response.

      The read timeout begins once the first response read is triggered after the server response is received. This timeout triggers periodically but won't fire its operation if another read operation has completed between when the timeout is triggered and completes.

      If readTimeout is null or Configuration.PROPERTY_AZURE_REQUEST_READ_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no timeout period will be applied to response read. When applying the timeout the greatest of one millisecond and the value of readTimeout will be used.

      Parameters:
      readTimeout - Read timeout duration.
      Returns:
      The updated JdkHttpClientBuilder object.
    • proxy

      public JdkHttpClientBuilder proxy(ProxyOptions proxyOptions)
      Sets the proxy.

      Code Samples

       final String proxyHost = "<proxy-host>"; // e.g. localhost
       final int proxyPort = 9999; // Proxy port
       ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,
               new InetSocketAddress(proxyHost, proxyPort));
       HttpClient client = new JdkHttpClientBuilder()
               .proxy(proxyOptions)
               .build();
       
      Parameters:
      proxyOptions - The proxy configuration to use.
      Returns:
      the updated JdkHttpClientBuilder object
    • configuration

      public JdkHttpClientBuilder configuration(Configuration configuration)
      Sets the configuration store that is used during construction of the HTTP client.

      The default configuration store is a clone of the global configuration store, use Configuration.NONE to bypass using configuration settings during construction.

      Parameters:
      configuration - The configuration store used to
      Returns:
      The updated JdkHttpClientBuilder object.
    • build

      public HttpClient build()
      Build a HttpClient with current configurations.
      Returns:
      a HttpClient.