Search in sources :

Example 91 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project jersey by jersey.

the class ApacheConnector method createConnectionManager.

private HttpClientConnectionManager createConnectionManager(final Client client, final Configuration config, final SSLContext sslContext, final boolean useSystemProperties) {
    final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols")) : null;
    final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites")) : null;
    HostnameVerifier hostnameVerifier = client.getHostnameVerifier();
    final LayeredConnectionSocketFactory sslSocketFactory;
    if (sslContext != null) {
        sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
    } else {
        if (useSystemProperties) {
            sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
        } else {
            sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
        }
    }
    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
    final Integer chunkSize = ClientProperties.getValue(config.getProperties(), ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.class);
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry, new ConnectionFactory(chunkSize));
    if (useSystemProperties) {
        String s = System.getProperty("http.keepAlive", "true");
        if ("true".equalsIgnoreCase(s)) {
            s = System.getProperty("http.maxConnections", "5");
            final int max = Integer.parseInt(s);
            connectionManager.setDefaultMaxPerRoute(max);
            connectionManager.setMaxTotal(2 * max);
        }
    }
    return connectionManager;
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) ManagedHttpClientConnectionFactory(org.apache.http.impl.conn.ManagedHttpClientConnectionFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 92 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project jersey by jersey.

the class HttpMethodTest method createPoolingClient.

protected Client createPoolingClient() {
    ClientConfig cc = new ClientConfig();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(100);
    connectionManager.setDefaultMaxPerRoute(100);
    cc.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    cc.connectorProvider(new ApacheConnectorProvider());
    return ClientBuilder.newClient(cc);
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 93 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project jersey by jersey.

the class HelloWorldTest method testAsyncClientRequests.

@Test
public void testAsyncClientRequests() throws InterruptedException {
    HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    ClientConfig cc = new ClientConfig();
    cc.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget target = client.target(getBaseUri());
    final int REQUESTS = 20;
    final CountDownLatch latch = new CountDownLatch(REQUESTS);
    final long tic = System.currentTimeMillis();
    final Map<Integer, String> results = new ConcurrentHashMap<Integer, String>();
    for (int i = 0; i < REQUESTS; i++) {
        final int id = i;
        target.path(ROOT_PATH).request().async().get(new InvocationCallback<Response>() {

            @Override
            public void completed(Response response) {
                try {
                    final String result = response.readEntity(String.class);
                    results.put(id, result);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void failed(Throwable error) {
                Logger.getLogger(HelloWorldTest.class.getName()).log(Level.SEVERE, "Failed on throwable", error);
                results.put(id, "error: " + error.getMessage());
                latch.countDown();
            }
        });
    }
    assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
    final long toc = System.currentTimeMillis();
    Logger.getLogger(HelloWorldTest.class.getName()).info("Executed in: " + (toc - tic));
    StringBuilder resultInfo = new StringBuilder("Results:\n");
    for (int i = 0; i < REQUESTS; i++) {
        String result = results.get(i);
        resultInfo.append(i).append(": ").append(result).append('\n');
    }
    Logger.getLogger(HelloWorldTest.class.getName()).info(resultInfo.toString());
    for (int i = 0; i < REQUESTS; i++) {
        String result = results.get(i);
        assertEquals(HelloWorldResource.CLICHED_MESSAGE, result);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Response(javax.ws.rs.core.Response) HttpResponse(org.apache.http.HttpResponse) WebTarget(javax.ws.rs.client.WebTarget) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 94 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project jersey by jersey.

the class ItemStoreResourceTest method configureClient.

@Override
protected void configureClient(ClientConfig config) {
    // using AHC as a test client connector to avoid issues with HttpUrlConnection socket management.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // adjusting max. connections just to be safe - the testEventSourceReconnect is quite greedy...
    cm.setMaxTotal(MAX_LISTENERS * MAX_ITEMS);
    cm.setDefaultMaxPerRoute(MAX_LISTENERS * MAX_ITEMS);
    config.register(SseFeature.class).property(ApacheClientProperties.CONNECTION_MANAGER, cm).property(ClientProperties.READ_TIMEOUT, 2000).connectorProvider(new ApacheConnectorProvider());
}
Also used : ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) SseFeature(org.glassfish.jersey.media.sse.SseFeature) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 95 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutor method createDefaultHttpClient.

private static HttpClient createDefaultHttpClient() {
    Registry<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    return HttpClientBuilder.create().setConnectionManager(connectionManager).build();
}
Also used : PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12