Search in sources :

Example 1 with BasicPingHandler

use of org.apache.hc.core5.http2.nio.support.BasicPingHandler in project httpcomponents-core by apache.

the class H2IntegrationTest method testConnectionPing.

@Test
public void testConnectionPing() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start();
    client.start();
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final int n = 10;
    final CountDownLatch latch = new CountDownLatch(n);
    final AtomicInteger count = new AtomicInteger(0);
    for (int i = 0; i < n; i++) {
        streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        streamEndpoint.execute(new PingCommand(new BasicPingHandler(result -> {
            if (result) {
                count.incrementAndGet();
            }
            latch.countDown();
        })), Command.Priority.NORMAL);
    }
    Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
    Assertions.assertEquals(n, count.get());
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) BasicPingHandler(org.apache.hc.core5.http2.nio.support.BasicPingHandler) CountDownLatch(java.util.concurrent.CountDownLatch) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) Test(org.junit.Test)

Example 2 with BasicPingHandler

use of org.apache.hc.core5.http2.nio.support.BasicPingHandler in project httpcomponents-core by apache.

the class H2ConnPool method validateSession.

@Override
protected void validateSession(final IOSession ioSession, final Callback<Boolean> callback) {
    if (ioSession.isOpen()) {
        final TimeValue timeValue = validateAfterInactivity;
        if (TimeValue.isNonNegative(timeValue)) {
            final long lastAccessTime = Math.min(ioSession.getLastReadTime(), ioSession.getLastWriteTime());
            final long deadline = lastAccessTime + timeValue.toMilliseconds();
            if (deadline <= System.currentTimeMillis()) {
                final Timeout socketTimeoutMillis = ioSession.getSocketTimeout();
                ioSession.enqueue(new PingCommand(new BasicPingHandler(result -> {
                    ioSession.setSocketTimeout(socketTimeoutMillis);
                    callback.execute(result);
                })), Command.Priority.NORMAL);
                return;
            }
        }
        callback.execute(true);
    } else {
        callback.execute(false);
    }
}
Also used : Timeout(org.apache.hc.core5.util.Timeout) BasicPingHandler(org.apache.hc.core5.http2.nio.support.BasicPingHandler) TimeValue(org.apache.hc.core5.util.TimeValue) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand)

Aggregations

PingCommand (org.apache.hc.core5.http2.nio.command.PingCommand)2 BasicPingHandler (org.apache.hc.core5.http2.nio.support.BasicPingHandler)2 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 TimeValue (org.apache.hc.core5.util.TimeValue)1 Timeout (org.apache.hc.core5.util.Timeout)1 Test (org.junit.Test)1