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;
}
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;
}
}
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));
}
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));
}
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());
}
Aggregations