Search in sources :

Example 6 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project camel by apache.

the class AhcEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (client == null) {
        AsyncHttpClientConfig config = null;
        if (clientConfig != null) {
            DefaultAsyncHttpClientConfig.Builder builder = AhcComponent.cloneConfig(clientConfig);
            if (sslContextParameters != null) {
                SSLContext sslContext = sslContextParameters.createSSLContext(getCamelContext());
                JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
                builder.setSslContext(ssl);
            }
            config = builder.build();
        } else {
            if (sslContextParameters != null) {
                DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
                SSLContext sslContext = sslContextParameters.createSSLContext(getCamelContext());
                JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
                builder.setSslContext(ssl);
                config = builder.build();
            }
        }
        client = createClient(config);
    }
}
Also used : JdkSslContext(io.netty.handler.ssl.JdkSslContext) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) SSLContext(javax.net.ssl.SSLContext)

Example 7 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class RetryNonBlockingIssue method testRetryNonBlockingAsyncConnect.

@Test(groups = "standalone")
public void testRetryNonBlockingAsyncConnect() throws IOException, InterruptedException, ExecutionException {
    AsyncHttpClientConfig config = //
    config().setKeepAlive(//
    true).setMaxConnections(//
    100).setConnectTimeout(//
    60000).setRequestTimeout(//
    30000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> res = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
        }
        StringBuilder b = new StringBuilder();
        for (ListenableFuture<Response> r : res) {
            Response theres = r.get();
            assertEquals(theres.getStatusCode(), 200);
            b.append("==============\r\n");
            b.append("Response Headers\r\n");
            HttpHeaders heads = theres.getHeaders();
            b.append(heads + "\r\n");
            b.append("==============\r\n");
        }
        System.out.println(b.toString());
        System.out.flush();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 8 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class RetryNonBlockingIssue method testRetryNonBlocking.

/**
     * Tests that a head request can be made
     * 
     * @throws IOException
     * @throws ExecutionException
     * @throws InterruptedException
     */
@Test(groups = "standalone")
public void testRetryNonBlocking() throws IOException, InterruptedException, ExecutionException {
    AsyncHttpClientConfig config = //
    config().setKeepAlive(//
    true).setMaxConnections(//
    100).setConnectTimeout(//
    60000).setRequestTimeout(//
    30000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> res = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            res.add(testMethodRequest(client, 3, "servlet", UUID.randomUUID().toString()));
        }
        StringBuilder b = new StringBuilder();
        for (ListenableFuture<Response> r : res) {
            Response theres = r.get();
            assertEquals(200, theres.getStatusCode());
            b.append("==============\r\n");
            b.append("Response Headers\r\n");
            HttpHeaders heads = theres.getHeaders();
            b.append(heads + "\r\n");
            b.append("==============\r\n");
        }
        System.out.println(b.toString());
        System.out.flush();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 9 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class MaxTotalConnectionTest method testMaxTotalConnections.

@Test(groups = "online")
public void testMaxTotalConnections() throws Exception {
    String[] urls = new String[] { "http://google.com", "http://gatling.io" };
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicReference<Throwable> ex = new AtomicReference<>();
    final AtomicReference<String> failedUrl = new AtomicReference<>();
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    1000).setRequestTimeout(//
    5000).setKeepAlive(//
    false).setMaxConnections(//
    2).setMaxConnectionsPerHost(//
    1).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        for (String url : urls) {
            final String thisUrl = url;
            client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    Response r = super.onCompleted(response);
                    latch.countDown();
                    return r;
                }

                @Override
                public void onThrowable(Throwable t) {
                    super.onThrowable(t);
                    ex.set(t);
                    failedUrl.set(thisUrl);
                    latch.countDown();
                }
            });
        }
        latch.await();
        assertNull(ex.get());
        assertNull(failedUrl.get());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Response(org.asynchttpclient.Response) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 10 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class MaxTotalConnectionTest method testMaxTotalConnectionsExceedingException.

@Test(groups = "online")
public void testMaxTotalConnectionsExceedingException() throws IOException {
    String[] urls = new String[] { "http://google.com", "http://github.com/" };
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    1000).setRequestTimeout(//
    5000).setKeepAlive(//
    false).setMaxConnections(//
    1).setMaxConnectionsPerHost(//
    1).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        List<ListenableFuture<Response>> futures = new ArrayList<>();
        for (String url : urls) {
            futures.add(client.prepareGet(url).execute());
        }
        boolean caughtError = false;
        int i;
        for (i = 0; i < urls.length; i++) {
            try {
                futures.get(i).get();
            } catch (Exception e) {
                // assert that 2nd request fails, because
                // maxTotalConnections=1
                caughtError = true;
                break;
            }
        }
        Assert.assertEquals(1, i);
        Assert.assertTrue(caughtError);
    }
}
Also used : ArrayList(java.util.ArrayList) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) ListenableFuture(org.asynchttpclient.ListenableFuture) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Aggregations

AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)9 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)7 Test (org.testng.annotations.Test)7 Response (org.asynchttpclient.Response)6 JdkSslContext (io.netty.handler.ssl.JdkSslContext)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SSLContext (javax.net.ssl.SSLContext)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)3 ListenableFuture (org.asynchttpclient.ListenableFuture)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)2 AsyncCompletionHandlerBase (org.asynchttpclient.AsyncCompletionHandlerBase)2 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1