Search in sources :

Example 16 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class DuplexConnectionPool method sweep.

@Override
public boolean sweep() {
    List<Connection> toSweep;
    lock();
    try {
        toSweep = activeConnections.stream().filter(connection -> connection instanceof Sweeper.Sweepable).collect(Collectors.toList());
    } finally {
        unlock();
    }
    for (Connection connection : toSweep) {
        if (((Sweeper.Sweepable) connection).sweep()) {
            boolean removed = remove(connection, true);
            LOG.warn("Connection swept: {}{}{} from active connections{}{}", connection, System.lineSeparator(), removed ? "Removed" : "Not removed", System.lineSeparator(), dump());
        }
    }
    return false;
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Sweeper(org.eclipse.jetty.util.thread.Sweeper)

Example 17 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpDestination method process.

private void process() {
    while (true) {
        Connection connection = connectionPool.acquire();
        if (connection == null)
            break;
        boolean proceed = process(connection);
        if (!proceed)
            break;
    }
}
Also used : Connection(org.eclipse.jetty.client.api.Connection)

Example 18 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpClientTest method testRequestSentOnlyAfterConnectionOpen.

@Test
public void testRequestSentOnlyAfterConnectionOpen() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    final AtomicBoolean open = new AtomicBoolean();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

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

                @Override
                public void onOpen() {
                    open.set(true);
                    super.onOpen();
                }
            };
        }
    }, sslContextFactory);
    client.start();
    final CountDownLatch latch = new CountDownLatch(2);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
        Assert.assertTrue(open.get());
        latch.countDown();
    }).send(result -> {
        if (result.isSucceeded())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) IOException(java.io.IOException) EndPoint(org.eclipse.jetty.io.EndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FuturePromise(org.eclipse.jetty.util.FuturePromise) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test)

Example 19 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpClientExplicitConnectionTest method testExplicitConnectionResponseListeners.

@Test
public void testExplicitConnectionResponseListeners() throws Exception {
    start(new EmptyServerHandler());
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).onResponseSuccess(response -> responseLatch.countDown());
    FutureResponseListener listener = new FutureResponseListener(request);
    connection.send(request, listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) CountDownLatch(java.util.concurrent.CountDownLatch) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 20 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpClientFailureTest method testFailureAfterRequestCommit.

@Test
public void testFailureAfterRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    final CountDownLatch commitLatch = new CountDownLatch(1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).onRequestCommit(request -> {
        connectionRef.get().getEndPoint().close();
        commitLatch.countDown();
    }).content(content).idleTimeout(2, TimeUnit.SECONDS).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    final CountDownLatch contentLatch = new CountDownLatch(1);
    content.offer(ByteBuffer.allocate(1024), new Callback() {

        @Override
        public void failed(Throwable x) {
            contentLatch.countDown();
        }
    });
    Assert.assertTrue(commitLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Handler(org.eclipse.jetty.server.Handler) EndPoint(org.eclipse.jetty.io.EndPoint) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) After(org.junit.After) Assert(org.junit.Assert) Server(org.eclipse.jetty.server.Server) Callback(org.eclipse.jetty.util.Callback) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Test(org.junit.Test)

Aggregations

Connection (org.eclipse.jetty.client.api.Connection)40 Test (org.junit.Test)33 CountDownLatch (java.util.concurrent.CountDownLatch)23 Request (org.eclipse.jetty.client.api.Request)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 Origin (org.eclipse.jetty.client.Origin)13 Result (org.eclipse.jetty.client.api.Result)13 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)13 Promise (org.eclipse.jetty.util.Promise)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)9 Destination (org.eclipse.jetty.client.api.Destination)9 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)9 ServletException (javax.servlet.ServletException)8 Response (org.eclipse.jetty.client.api.Response)8 TimeUnit (java.util.concurrent.TimeUnit)7 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)7 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)7