Search in sources :

Example 6 with HttpClientTransportOverHTTP

use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project jetty.project by eclipse.

the class HttpClientUploadDuringServerShutdown method testUploadDuringServerShutdown.

@Test
public void testUploadDuringServerShutdown() throws Exception {
    final AtomicReference<EndPoint> endPointRef = new AtomicReference<>();
    final CountDownLatch serverLatch = new CountDownLatch(1);
    QueuedThreadPool serverThreads = new QueuedThreadPool();
    serverThreads.setName("server");
    Server server = new Server(serverThreads);
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            endPointRef.set(baseRequest.getHttpChannel().getEndPoint());
            serverLatch.countDown();
        }
    });
    server.start();
    final AtomicBoolean afterSetup = new AtomicBoolean();
    final CountDownLatch sendLatch = new CountDownLatch(1);
    final CountDownLatch beginLatch = new CountDownLatch(1);
    final CountDownLatch associateLatch = new CountDownLatch(1);
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    HttpClient client = new HttpClient(new HttpClientTransportOverHTTP(1) {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            return new HttpConnectionOverHTTP(endPoint, destination, promise) {

                @Override
                protected HttpChannelOverHTTP newHttpChannel() {
                    return new HttpChannelOverHTTP(this) {

                        @Override
                        public void send() {
                            if (afterSetup.get()) {
                                associateLatch.countDown();
                            }
                            super.send();
                        }
                    };
                }

                @Override
                protected void close(Throwable failure) {
                    try {
                        sendLatch.countDown();
                        beginLatch.await(5, TimeUnit.SECONDS);
                        super.close(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                    }
                }

                @Override
                protected boolean abort(Throwable failure) {
                    try {
                        associateLatch.await(5, TimeUnit.SECONDS);
                        return super.abort(failure);
                    } catch (InterruptedException x) {
                        x.printStackTrace();
                        return false;
                    }
                }
            };
        }
    }, null);
    client.setIdleTimeout(10000);
    client.setExecutor(clientThreads);
    client.start();
    // Create one connection.
    client.newRequest("localhost", connector.getLocalPort()).send();
    Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
    afterSetup.set(true);
    Thread.sleep(1000);
    // Close the connection, so that the receiver is woken
    // up and will call HttpConnectionOverHTTP.close().
    EndPoint endPoint = endPointRef.get();
    endPoint.close();
    // Wait for close() so that the connection that
    // is being closed is used to send the request.
    Assert.assertTrue(sendLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).timeout(10, TimeUnit.SECONDS).onRequestBegin(request -> {
        try {
            beginLatch.countDown();
            completeLatch.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException x) {
            x.printStackTrace();
        }
    }).send(result -> completeLatch.countDown());
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", connector.getLocalPort());
    DuplexConnectionPool pool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, pool.getConnectionCount());
    Assert.assertEquals(0, pool.getIdleConnections().size());
    Assert.assertEquals(0, pool.getActiveConnections().size());
}
Also used : Request(org.eclipse.jetty.server.Request) Connection(org.eclipse.jetty.client.api.Connection) EndPoint(org.eclipse.jetty.io.EndPoint) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Server(org.eclipse.jetty.server.Server) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConnector(org.eclipse.jetty.server.ServerConnector) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InputStream(java.io.InputStream) Server(org.eclipse.jetty.server.Server) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpChannelOverHTTP(org.eclipse.jetty.client.http.HttpChannelOverHTTP) Test(org.junit.Test)

Example 7 with HttpClientTransportOverHTTP

use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project jetty.project by eclipse.

the class ServerConnectionCloseTest method startClient.

private void startClient() throws Exception {
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    client = new HttpClient(new HttpClientTransportOverHTTP(1), null);
    client.setExecutor(clientThreads);
    client.start();
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)

Example 8 with HttpClientTransportOverHTTP

use of org.eclipse.jetty.client.http.HttpClientTransportOverHTTP in project instrumentation-java by census-instrumentation.

the class HelloWorldClient method main.

/**
 * HelloWorldClient sends http request periodically to {@link HelloWorldServer}. These requests
 * are instrumented using opencensus jetty client library to enable tracing and monitoring stats.
 */
public static void main(String[] args) throws Exception {
    BasicConfigurator.configure();
    initTracing();
    initStatsExporter();
    // Create http client that will trace requests. By default trace context is propagated using
    // w3c TraceContext propagator.
    // To use B3 propagation use following
    // OcJettyHttpClient httpClient =
    // new OcJettyHttpClient(
    // new HttpClientTransportOverHTTP(),
    // new SslContextFactory(),
    // null,
    // Tracing.getPropagationComponent().getB3Format());
    OcJettyHttpClient httpClient = new OcJettyHttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(), null, null);
    httpClient.start();
    do {
        HttpRequest request = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request").method(HttpMethod.GET);
        HttpRequest asyncRequest = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request/async").method(HttpMethod.GET);
        HttpRequest postRequest = (HttpRequest) httpClient.newRequest("http://localhost:8080/helloworld/request").method(HttpMethod.POST);
        postRequest.content(new StringContentProvider("{\"hello\": \"world\"}"), "application/json");
        if (request == null) {
            logger.info("Request is null");
            break;
        }
        request.send();
        asyncRequest.send();
        postRequest.send();
        try {
            Thread.sleep(15000);
        } catch (Exception e) {
            logger.error("Error while sleeping");
        }
    } while (true);
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) OcJettyHttpClient(io.opencensus.contrib.http.jetty.client.OcJettyHttpClient) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) IOException(java.io.IOException)

Aggregations

HttpClientTransportOverHTTP (org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)8 EndPoint (org.eclipse.jetty.io.EndPoint)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)4 Promise (org.eclipse.jetty.util.Promise)4 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeUnit (java.util.concurrent.TimeUnit)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ServletException (javax.servlet.ServletException)3 Connection (org.eclipse.jetty.client.api.Connection)3 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)3 Assert (org.junit.Assert)3 InputStream (java.io.InputStream)2 ByteBuffer (java.nio.ByteBuffer)2 Random (java.util.Random)2