use of org.eclipse.jetty.io.Connection in project druid by druid-io.
the class JettyMonitoringConnectionFactory method newConnection.
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
final Connection connection = connectionFactory.newConnection(connector, endPoint);
connection.addListener(new Connection.Listener() {
@Override
public void onOpened(Connection connection) {
activeConns.incrementAndGet();
}
@Override
public void onClosed(Connection connection) {
activeConns.decrementAndGet();
}
});
return connection;
}
use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.
the class LocalConnector method accept.
@Override
protected void accept(int acceptorID) throws IOException, InterruptedException {
if (LOG.isDebugEnabled())
LOG.debug("accepting {}", acceptorID);
LocalEndPoint endPoint = _connects.take();
endPoint.onOpen();
onEndPointOpened(endPoint);
Connection connection = getDefaultConnectionFactory().newConnection(this, endPoint);
endPoint.setConnection(connection);
connection.onOpen();
}
use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.
the class SslConnectionFactory method newConnection.
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
SSLEngine engine = _sslContextFactory.newSSLEngine(endPoint.getRemoteAddress());
engine.setUseClientMode(false);
SslConnection sslConnection = newSslConnection(connector, endPoint, engine);
sslConnection.setRenegotiationAllowed(_sslContextFactory.isRenegotiationAllowed());
configure(sslConnection, connector, endPoint);
ConnectionFactory next = connector.getConnectionFactory(_nextProtocol);
EndPoint decryptedEndPoint = sslConnection.getDecryptedEndPoint();
Connection connection = next.newConnection(connector, decryptedEndPoint);
decryptedEndPoint.setConnection(connection);
return sslConnection;
}
use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncReadIdleTimeout.
@Test
public void testAsyncReadIdleTimeout() throws Exception {
int status = 567;
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
AsyncContext asyncContext = request.startAsync(request, response);
asyncContext.setTimeout(0);
ServletInputStream inputStream = request.getInputStream();
inputStream.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
while (inputStream.isReady() && !inputStream.isFinished()) inputStream.read();
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
}
@Override
public void onError(Throwable t) {
assertScope();
response.setStatus(status);
// Do not put Connection: close header here, the test
// verifies that the server closes no matter what.
asyncContext.complete();
}
});
}
});
connector.setIdleTimeout(1000);
CountDownLatch closeLatch = new CountDownLatch(1);
connector.addBean(new Connection.Listener() {
@Override
public void onOpened(Connection connection) {
}
@Override
public void onClosed(Connection connection) {
closeLatch.countDown();
}
});
String data = "0123456789";
DeferredContentProvider content = new DeferredContentProvider();
content.offer(ByteBuffer.wrap(data.getBytes(StandardCharsets.UTF_8)));
CountDownLatch responseLatch = new CountDownLatch(1);
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(content).onResponseSuccess(r -> responseLatch.countDown()).timeout(5, TimeUnit.SECONDS).send(result -> {
assertEquals(status, result.getResponse().getStatus());
clientLatch.countDown();
});
assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
content.close();
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.io.Connection in project jetty.project by eclipse.
the class ConnectionOpenCloseTest method testOpenRequestClose.
@Slow
@Test
public void testOpenRequestClose() throws Exception {
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
}
});
server.start();
final AtomicInteger callbacks = new AtomicInteger();
final CountDownLatch openLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
connector.addBean(new Connection.Listener.Adapter() {
@Override
public void onOpened(Connection connection) {
callbacks.incrementAndGet();
openLatch.countDown();
}
@Override
public void onClosed(Connection connection) {
callbacks.incrementAndGet();
closeLatch.countDown();
}
});
try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
socket.setSoTimeout((int) connector.getIdleTimeout());
OutputStream output = socket.getOutputStream();
output.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
output.flush();
InputStream inputStream = socket.getInputStream();
HttpTester.Response response = HttpTester.parseResponse(inputStream);
assertThat("Status Code", response.getStatus(), is(200));
Assert.assertEquals(-1, inputStream.read());
socket.close();
Assert.assertTrue(openLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
// Wait some time to see if the callbacks are called too many times
TimeUnit.SECONDS.sleep(1);
Assert.assertEquals(2, callbacks.get());
}
}
Aggregations