Search in sources :

Example 6 with AsyncHttpClient

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

the class WebsocketTwoRoutesExampleTest method testWSHttpCallEcho.

@Test
public void testWSHttpCallEcho() throws Exception {
    // We call the route WebSocket BAR
    received.clear();
    latch = new CountDownLatch(1);
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/bar").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("The bar has Beer", received.get(0));
    websocket.close();
    c.close();
    // We call the route WebSocket PUB
    received.clear();
    latch = new CountDownLatch(1);
    c = new DefaultAsyncHttpClient();
    websocket = c.prepareGet("ws://localhost:" + port + "/pub").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("wine");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("The pub has wine", received.get(0));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketUpgradeHandler(org.asynchttpclient.ws.WebSocketUpgradeHandler) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 7 with AsyncHttpClient

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

the class WebsocketTwoRoutesToSIndividualAndBroadcastEndpointExampleTest method testWSHttpCallEcho.

@Test
public void testWSHttpCallEcho() throws Exception {
    // We call the route WebSocket BAR
    received.clear();
    latch = new CountDownLatch(2);
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/bar").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(2, received.size());
    //Cannot guarantee the order in which messages are received
    assertTrue(received.contains("The bar has Beer"));
    assertTrue(received.contains("Broadcasting to Bar"));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 8 with AsyncHttpClient

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

the class WebsocketConsumerRouteTest method testWSBytesHttpCall.

@Test
public void testWSBytesHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + port + "/echo").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    MockEndpoint result = getMockEndpoint("mock:result");
    final byte[] testmessage = "Test".getBytes("utf-8");
    result.expectedBodiesReceived(testmessage);
    websocket.sendMessage(testmessage);
    result.assertIsSatisfied();
    websocket.close();
    c.close();
}
Also used : WebSocketListener(org.asynchttpclient.ws.WebSocketListener) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 9 with AsyncHttpClient

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

the class WebsocketConsumerRouteTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + port + "/echo").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedBodiesReceived("Test");
    websocket.sendMessage("Test");
    result.assertIsSatisfied();
    websocket.close();
    c.close();
}
Also used : WebSocketListener(org.asynchttpclient.ws.WebSocketListener) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 10 with AsyncHttpClient

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

the class AsyncHttpTest method testPromiseAdapter.

public void testPromiseAdapter() throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger successCount = new AtomicInteger();
    final AtomicInteger progressCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient()) {
        Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject.promise(client.prepareGet("http://gatling.io"));
        p1.done(new DoneCallback<Response>() {

            @Override
            public void onDone(Response response) {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    successCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        }).progress(new ProgressCallback<HttpProgress>() {

            @Override
            public void onProgress(HttpProgress progress) {
                progressCount.incrementAndGet();
            }
        });
        latch.await();
        assertTrue(progressCount.get() > 0);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoneCallback(org.jdeferred.DoneCallback) HttpProgress(org.asynchttpclient.extras.jdeferred.HttpProgress) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7