Search in sources :

Example 6 with AsyncHttpClient

use of com.ning.http.client.AsyncHttpClient in project cdap by caskdata.

the class NettyRouterTestBase method testRouterAsync.

@Test
public void testRouterAsync() throws Exception {
    int numElements = 123;
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    final CountDownLatch latch = new CountDownLatch(numElements);
    final AtomicInteger numSuccessfulRequests = new AtomicInteger(0);
    for (int i = 0; i < numElements; ++i) {
        final int elem = i;
        final Request request = new RequestBuilder("GET").setUrl(resolveURI(DEFAULT_SERVICE, String.format("%s/%s-%d", "/v1/echo", "async", i))).build();
        asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

            @Override
            public Void onCompleted(Response response) throws Exception {
                latch.countDown();
                Assert.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
                String responseBody = response.getResponseBody();
                LOG.trace("Got response {}", responseBody);
                Assert.assertEquals("async-" + elem, responseBody);
                numSuccessfulRequests.incrementAndGet();
                return null;
            }

            @Override
            public void onThrowable(Throwable t) {
                LOG.error("Got exception while posting {}", elem, t);
                latch.countDown();
            }
        });
        // Sleep so as not to overrun the server.
        TimeUnit.MILLISECONDS.sleep(1);
    }
    latch.await();
    asyncHttpClient.close();
    Assert.assertEquals(numElements, numSuccessfulRequests.get());
    // we use sticky endpoint strategy so the sum of requests from the two gateways should be NUM_ELEMENTS
    Assert.assertTrue(numElements == (defaultServer1.getNumRequests() + defaultServer2.getNumRequests()));
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(com.ning.http.client.Response) HttpResponse(org.apache.http.HttpResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 7 with AsyncHttpClient

use of com.ning.http.client.AsyncHttpClient in project cdap by caskdata.

the class NettyRouterTestBase method testUpload.

@Test
public void testUpload() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(resolveURI(DEFAULT_SERVICE, "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            // TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(com.ning.http.client.Response) HttpResponse(org.apache.http.HttpResponse) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 8 with AsyncHttpClient

use of com.ning.http.client.AsyncHttpClient in project Singularity by HubSpot.

the class SingularityExecutorModule method providesHttpClient.

@Provides
@Singleton
@Named(LOCAL_DOWNLOAD_HTTP_CLIENT)
public AsyncHttpClient providesHttpClient(SingularityExecutorConfiguration configuration) {
    AsyncHttpClientConfig.Builder configBldr = new AsyncHttpClientConfig.Builder();
    configBldr.setRequestTimeoutInMs((int) configuration.getLocalDownloadServiceTimeoutMillis());
    configBldr.setIdleConnectionTimeoutInMs((int) configuration.getLocalDownloadServiceTimeoutMillis());
    configBldr.addRequestFilter(new ThrottleRequestFilter(configuration.getLocalDownloadServiceMaxConnections()));
    return new AsyncHttpClient(configBldr.build());
}
Also used : ThrottleRequestFilter(com.ning.http.client.extra.ThrottleRequestFilter) Builder(com.spotify.docker.client.DefaultDockerClient.Builder) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Named(com.google.inject.name.Named) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 9 with AsyncHttpClient

use of com.ning.http.client.AsyncHttpClient in project OpenTripPlanner by opentripplanner.

the class WebsocketGtfsRealtimeUpdater method run.

@Override
public void run() throws InterruptedException {
    while (true) {
        AsyncHttpClient client = new AsyncHttpClient();
        WebSocketListener listener = new Listener();
        WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(listener).build();
        WebSocket socket = null;
        boolean connectionSuccessful = true;
        // Try to create a websocket connection
        try {
            socket = client.prepareGet(url).execute(handler).get();
            LOG.info("Successfully connected to {}.", url);
        } catch (ExecutionException e) {
            LOG.error("Could not connect to {}: {}", url, e.getCause().getMessage());
            connectionSuccessful = false;
        } catch (Exception e) {
            LOG.error("Unknown exception when trying to connect to {}:", url, e);
            connectionSuccessful = false;
        }
        // If connection was unsuccessful, wait some time before trying again
        if (!connectionSuccessful) {
            Thread.sleep(reconnectPeriodSec * 1000);
        }
        // Keep checking whether connection is still open
        while (true) {
            if (socket == null || !socket.isOpen()) {
                // The connection is closed somehow, try to reconnect
                if (connectionSuccessful) {
                    LOG.warn("Connection to {} was lost. Trying to reconnect...", url);
                }
                break;
            }
            Thread.sleep(CHECK_CONNECTION_PERIOD_SEC * 1000);
        }
        client.close();
    }
}
Also used : WebSocketListener(com.ning.http.client.websocket.WebSocketListener) DefaultWebSocketListener(com.ning.http.client.websocket.DefaultWebSocketListener) WebSocketListener(com.ning.http.client.websocket.WebSocketListener) DefaultWebSocketListener(com.ning.http.client.websocket.DefaultWebSocketListener) WebSocketUpgradeHandler(com.ning.http.client.websocket.WebSocketUpgradeHandler) ExecutionException(java.util.concurrent.ExecutionException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ExecutionException(java.util.concurrent.ExecutionException) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) WebSocket(com.ning.http.client.websocket.WebSocket)

Example 10 with AsyncHttpClient

use of com.ning.http.client.AsyncHttpClient in project rxrabbit by meltwater.

the class RxRabbitMultiNodeTest method setup.

@Before
public void setup() throws Exception {
    dockerContainers.resetAll(false);
    dockerContainers.rabbit(RABBIT_1).assertUp();
    dockerContainers.rabbit(RABBIT_2).assertUp();
    String rabbitTcpPort = dockerContainers.rabbit(RABBIT_1).tcpPort();
    rabbitAdminPort = dockerContainers.rabbit(RABBIT_1).adminPort();
    String rabbit2TcpPort = dockerContainers.rabbit(RABBIT_2).tcpPort();
    log.infoWithParams("****** Rabbit brokers are up and running *****");
    BrokerAddresses addresses = new BrokerAddresses("amqp://localhost:" + rabbitTcpPort + "," + "amqp://localhost:" + rabbit2TcpPort);
    channelFactory = new DefaultChannelFactory(addresses, connectionSettings);
    consumerFactory = new DefaultConsumerFactory(channelFactory, consumeSettings);
    DefaultPublisherFactory publisherFactory = new DefaultPublisherFactory(channelFactory, publishSettings);
    httpClient = new AsyncHttpClient();
    messagesSeen.clear();
    createQueues(channelFactory, inputQueue, new Exchange(inputExchange));
    publisher = publisherFactory.createPublisher();
}
Also used : DefaultChannelFactory(com.meltwater.rxrabbit.impl.DefaultChannelFactory) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Before(org.junit.Before)

Aggregations

AsyncHttpClient (com.ning.http.client.AsyncHttpClient)12 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)6 RequestBuilder (com.ning.http.client.RequestBuilder)5 Response (com.ning.http.client.Response)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 Request (com.ning.http.client.Request)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 URISyntaxException (java.net.URISyntaxException)3 DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)2 HttpResponseBodyPart (com.ning.http.client.HttpResponseBodyPart)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 AsyncHttpClient (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient)2 AsyncHttpClientConfigBean (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean)2 WebSocket (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket)2 WebSocketTextListener (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener)2