0
# Connection Management
1
2
Connection pooling, SSL/TLS configuration, and connection specifications for performance optimization.
3
4
## Capabilities
5
6
### ConnectionPool
7
8
Manages HTTP/HTTPS connection reuse to reduce latency and resource usage.
9
10
```java { .api }
11
public final class ConnectionPool {
12
public ConnectionPool(int maxIdleConnections, long keepAliveDurationMs);
13
public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit);
14
public static ConnectionPool getDefault();
15
public int getIdleConnectionCount();
16
public int getConnectionCount();
17
public int getSpdyConnectionCount();
18
public int getMultiplexedConnectionCount();
19
public int getHttpConnectionCount();
20
public void evictAll();
21
}
22
```
23
24
### ConnectionSpec
25
26
Configuration for socket connections including TLS versions and cipher suites.
27
28
```java { .api }
29
public final class ConnectionSpec {
30
public static final ConnectionSpec MODERN_TLS;
31
public static final ConnectionSpec COMPATIBLE_TLS;
32
public static final ConnectionSpec CLEARTEXT;
33
public boolean isTls();
34
public List<CipherSuite> cipherSuites();
35
public List<TlsVersion> tlsVersions();
36
public boolean supportsTlsExtensions();
37
public boolean isCompatible(SSLSocket socket);
38
}
39
```
40
41
### Dispatcher
42
43
Policy for executing asynchronous requests with thread pool management.
44
45
```java { .api }
46
public final class Dispatcher {
47
public Dispatcher();
48
public Dispatcher(ExecutorService executorService);
49
public ExecutorService getExecutorService();
50
public void setMaxRequests(int maxRequests);
51
public int getMaxRequests();
52
public void setMaxRequestsPerHost(int maxRequestsPerHost);
53
public int getMaxRequestsPerHost();
54
public void cancel(Object tag);
55
public int getRunningCallCount();
56
public int getQueuedCallCount();
57
}
58
```
59
60
**Usage Examples:**
61
62
```java
63
// Custom connection pool
64
ConnectionPool pool = new ConnectionPool(10, 5, TimeUnit.MINUTES);
65
client.setConnectionPool(pool);
66
67
// Modern TLS only
68
List<ConnectionSpec> specs = Arrays.asList(
69
ConnectionSpec.MODERN_TLS,
70
ConnectionSpec.CLEARTEXT
71
);
72
client.setConnectionSpecs(specs);
73
74
// Custom dispatcher limits
75
Dispatcher dispatcher = new Dispatcher();
76
dispatcher.setMaxRequests(100);
77
dispatcher.setMaxRequestsPerHost(10);
78
client.setDispatcher(dispatcher);
79
```