Search in sources :

Example 11 with DefaultAsyncHttpClient

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

the class WebsocketComponentRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/echo").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("BeerBeer", received.get(0));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) 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 12 with DefaultAsyncHttpClient

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

the class WebsocketClientCamelRouteTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + port + "/test").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();
    getMockEndpoint("mock:client").expectedBodiesReceived("Hello from WS client");
    websocket.sendMessage("Hello from WS client");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertMockEndpointsSatisfied();
    assertEquals(10, received.size());
    for (int i = 0; i < 10; i++) {
        assertEquals(">> Welcome on board!", received.get(i));
    }
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) 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 13 with DefaultAsyncHttpClient

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

the class WebsocketProducerRouteRestartTest method doTestWSHttpCall.

private void doTestWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/shop").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();
    // Send message to the direct endpoint
    producer.sendBodyAndHeader("Beer on stock at Apache Mall", WebsocketConstants.SEND_TO_ALL, "true");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    Object r = received.get(0);
    assertTrue(r instanceof String);
    assertEquals("Beer on stock at Apache Mall", r);
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket)

Example 14 with DefaultAsyncHttpClient

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

the class WebsocketRouteExampleTest 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 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("BeerBeer", received.get(0));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) 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 15 with DefaultAsyncHttpClient

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

the class WsEndpoint method createClient.

@Override
protected AsyncHttpClient createClient(AsyncHttpClientConfig config) {
    AsyncHttpClient client;
    if (config == null) {
        config = new DefaultAsyncHttpClientConfig.Builder().build();
        client = new DefaultAsyncHttpClient(config);
    } else {
        client = new DefaultAsyncHttpClient(config);
    }
    return client;
}
Also used : DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient)

Aggregations

DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)15 RouteBuilder (org.apache.camel.builder.RouteBuilder)13 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)13 WebSocket (org.asynchttpclient.ws.WebSocket)11 Test (org.junit.Test)10 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)8 CountDownLatch (java.util.concurrent.CountDownLatch)3 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)3 JdkSslContext (io.netty.handler.ssl.JdkSslContext)2 SSLContext (javax.net.ssl.SSLContext)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)2 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)2 WebSocketListener (org.asynchttpclient.ws.WebSocketListener)2 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 AhcHttpClient (com.github.scribejava.httpclient.ahc.AhcHttpClient)1