Search in sources :

Example 26 with ClientConnection

use of io.undertow.client.ClientConnection in project http2client-benchmark by networknt.

the class Http2ClientExample method testMultipleHttp2Get.

public void testMultipleHttp2Get(int round) throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(round);
    final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < round; i++) {
                    AtomicReference<ClientResponse> reference = new AtomicReference<>();
                    references.add(i, reference);
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/get");
                    request.getRequestHeaders().put(Headers.HOST, "localhost");
                    connection.sendRequest(request, client.createClientCallback(reference, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
    /*
            for (final AtomicReference<ClientResponse> reference : references) {
                System.out.println(reference.get().getAttachment(Http2Client.RESPONSE_BODY));
                System.out.println(reference.get().getProtocol().toString());
            }
            */
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 27 with ClientConnection

use of io.undertow.client.ClientConnection in project http2client-benchmark by networknt.

the class Http2ClientExample method testMultipleHttp2Post.

public void testMultipleHttp2Post(int round) throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(round);
    final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < round; i++) {
                    AtomicReference<ClientResponse> reference = new AtomicReference<>();
                    references.add(i, reference);
                    final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/post");
                    request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
                    request.getRequestHeaders().put(Headers.HOST, "localhost");
                    connection.sendRequest(request, client.createClientCallback(reference, latch, "post"));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
    /*
            for (final AtomicReference<ClientResponse> reference : references) {
                System.out.println(reference.get().getAttachment(Http2Client.RESPONSE_BODY));
                System.out.println(reference.get().getProtocol().toString());
            }
            */
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 28 with ClientConnection

use of io.undertow.client.ClientConnection in project light-portal by networknt.

the class CreateMenuTest method testCreateMenu.

@Test
public void testCreateMenu() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    System.out.println("\n");
    System.out.println("json:" + s);
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, s));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) SQLException(java.sql.SQLException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 29 with ClientConnection

use of io.undertow.client.ClientConnection in project light-portal by networknt.

the class GetMenuByHostTest method testGetMenuByHost.

// As we are using live database which is shared by multiple users, need to find a way to create test
// menu item and test menu before running this test and clean up later on. The created data need to have
// UUID as key so that multiple users can build and test at the same time. For now, just disable it.
// @Test
public void testGetMenuByHost() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    final String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"menu\",\"action\":\"getMenuByHost\",\"version\":\"0.1.0\",\"data\":{\"host\":\"example.com\"}}";
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, requestBody));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest)

Example 30 with ClientConnection

use of io.undertow.client.ClientConnection in project light-portal by networknt.

the class GetMenuTest method testGetMenu.

@Test
public void testGetMenu() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    final String requestBody = "{\"host\":\"lightapi.net\",\"service\":\"menu\",\"action\":\"getMenu\",\"version\":\"0.1.0\"}";
    System.out.println("json:" + requestBody);
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, requestBody));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Aggregations

ClientConnection (io.undertow.client.ClientConnection)119 ClientRequest (io.undertow.client.ClientRequest)112 CountDownLatch (java.util.concurrent.CountDownLatch)107 ClientResponse (io.undertow.client.ClientResponse)103 URI (java.net.URI)101 Test (org.junit.Test)99 AtomicReference (java.util.concurrent.atomic.AtomicReference)94 Http2Client (com.networknt.client.Http2Client)87 ClientException (com.networknt.exception.ClientException)82 ApiException (com.networknt.exception.ApiException)44 IOException (java.io.IOException)41 HttpString (io.undertow.util.HttpString)25 UndertowClient (io.undertow.client.UndertowClient)18 Status (com.networknt.status.Status)17 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)15 SQLException (java.sql.SQLException)12 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)9 ClientCallback (io.undertow.client.ClientCallback)6 ClientExchange (io.undertow.client.ClientExchange)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5