我想与Jersey 2.x休息客户端使用连接池。我有以下代码:
ClientConfig clientConfig = new ClientConfig();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
clientConfig.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newBuilder()
.withConfig(clientConfig)
.sslContext(SslConfigurator.getDefaultContext())
.build();
但这对需要客户端证书的网站不起作用。如果我不使用PoolingHttpClientConnectionManager
(即按照以下方式对其进行评论)
//clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
然后发送客户端证书。
如何设置PoolingHttpClientConnectionManager
以使用javax.net.ssl...
属性?
答案 0 :(得分:1)
使用配置了系统属性的SSL套接字工厂
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSystemSocketFactory())
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
答案 1 :(得分:0)
如果使用默认SSL上下文,则上述示例有效。 如果您有自定义SSL上下文,例如:
SslConfigurator sslConfig = SslConfigurator.newInstance()
.trustStoreFile(...).trustStorePassword(...)
.keyStoreFile(...).keyStorePassword(...).keyPassword(...);
SSLContext sslContext = sslConfig.createSSLContext();
然后将其提供给PoolingHttpClientConnectionManager:
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", new SSLConnectionSocketFactory(sslContext))
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
不再需要调用ClientBuilder.sslContext()。