Class HttpClientFactory

java.lang.Object
com.tnsai.utils.HttpClientFactory

public final class HttpClientFactory extends Object
Centralized factory for OkHttpClient instances.

This factory provides a shared HTTP client instance with sensible defaults, eliminating code duplication across LLM providers, executors, and tools.

Benefits:

  • Single connection pool for all HTTP operations
  • Proper resource cleanup via shutdown hook
  • Configurable timeouts with builder pattern
  • Thread-safe singleton pattern

Usage:


 // Get shared client with default settings
 OkHttpClient client = HttpClientFactory.getSharedClient();

 // Get client with custom timeout
 OkHttpClient customClient = HttpClientFactory.withTimeout(60, TimeUnit.SECONDS);

 // Get client with custom read timeout only
 OkHttpClient readClient = HttpClientFactory.withReadTimeout(120, TimeUnit.SECONDS);
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default timeout for connect, read, and write operations (in seconds)
    static final int
    Extended timeout for LLM operations that may take longer
  • Method Summary

    Modifier and Type
    Method
    Description
    static okhttp3.OkHttpClient
    Returns the shared HTTP client optimized for LLM operations.
    static okhttp3.OkHttpClient
    Returns the shared HTTP client with default timeout settings.
    static okhttp3.OkHttpClient
    withReadTimeout(long timeout, TimeUnit unit)
    Creates a new client with custom read timeout only.
    static okhttp3.OkHttpClient
    withTimeout(long timeout, TimeUnit unit)
    Creates a new client with custom timeout for all operations.
    static okhttp3.OkHttpClient
    withTimeouts(long connectTimeout, long readTimeout, long writeTimeout, TimeUnit unit)
    Creates a new client with custom timeouts for each operation.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_TIMEOUT_SECONDS

      public static final int DEFAULT_TIMEOUT_SECONDS
      Default timeout for connect, read, and write operations (in seconds)
      See Also:
    • LLM_TIMEOUT_SECONDS

      public static final int LLM_TIMEOUT_SECONDS
      Extended timeout for LLM operations that may take longer
      See Also:
  • Method Details

    • getSharedClient

      public static okhttp3.OkHttpClient getSharedClient()
      Returns the shared HTTP client with default timeout settings.

      Suitable for most API calls including web services and search tools. Default timeout is 30 seconds.

      Returns:
      shared OkHttpClient instance
    • getLLMClient

      public static okhttp3.OkHttpClient getLLMClient()
      Returns the shared HTTP client optimized for LLM operations.

      Uses extended timeout (300 seconds) suitable for LLM API calls which may take longer to respond.

      Returns:
      shared OkHttpClient instance for LLM operations
    • withTimeout

      public static okhttp3.OkHttpClient withTimeout(long timeout, TimeUnit unit)
      Creates a new client with custom timeout for all operations.

      The returned client shares the connection pool with the shared client but uses the specified timeout.

      Parameters:
      timeout - timeout value
      unit - time unit
      Returns:
      new OkHttpClient with custom timeout
    • withReadTimeout

      public static okhttp3.OkHttpClient withReadTimeout(long timeout, TimeUnit unit)
      Creates a new client with custom read timeout only.

      Useful for operations where connection is quick but response may take time.

      Parameters:
      timeout - read timeout value
      unit - time unit
      Returns:
      new OkHttpClient with custom read timeout
    • withTimeouts

      public static okhttp3.OkHttpClient withTimeouts(long connectTimeout, long readTimeout, long writeTimeout, TimeUnit unit)
      Creates a new client with custom timeouts for each operation.
      Parameters:
      connectTimeout - connect timeout value
      readTimeout - read timeout value
      writeTimeout - write timeout value
      unit - time unit for all timeouts
      Returns:
      new OkHttpClient with custom timeouts