Package com.tnsai.utils
Class HttpClientFactory
java.lang.Object
com.tnsai.utils.HttpClientFactory
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
FieldsModifier and TypeFieldDescriptionstatic final intDefault timeout for connect, read, and write operations (in seconds)static final intExtended timeout for LLM operations that may take longer -
Method Summary
Modifier and TypeMethodDescriptionstatic okhttp3.OkHttpClientReturns the shared HTTP client optimized for LLM operations.static okhttp3.OkHttpClientReturns the shared HTTP client with default timeout settings.static okhttp3.OkHttpClientwithReadTimeout(long timeout, TimeUnit unit) Creates a new client with custom read timeout only.static okhttp3.OkHttpClientwithTimeout(long timeout, TimeUnit unit) Creates a new client with custom timeout for all operations.static okhttp3.OkHttpClientwithTimeouts(long connectTimeout, long readTimeout, long writeTimeout, TimeUnit unit) Creates a new client with custom timeouts for each operation.
-
Field Details
-
DEFAULT_TIMEOUT_SECONDS
public static final int DEFAULT_TIMEOUT_SECONDSDefault timeout for connect, read, and write operations (in seconds)- See Also:
-
LLM_TIMEOUT_SECONDS
public static final int LLM_TIMEOUT_SECONDSExtended timeout for LLM operations that may take longer- See Also:
-
-
Method Details
-
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
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 valueunit- time unit- Returns:
- new OkHttpClient with custom timeout
-
withReadTimeout
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 valueunit- 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 valuereadTimeout- read timeout valuewriteTimeout- write timeout valueunit- time unit for all timeouts- Returns:
- new OkHttpClient with custom timeouts