org.eclipse.jetty.http2.api.server.ServerSessionListener Java Examples

The following examples show how to use org.eclipse.jetty.http2.api.server.ServerSessionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerConnectionFactory.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    LOG.info("newConnection: {}", endPoint.getLocalAddress().toString());
    ServerSessionListener listener = new CustomSessionListener(connector, endPoint, streamProcessors);

    Generator generator = new Generator(connector.getByteBufferPool(), getMaxDynamicTableSize(), getMaxHeaderBlockFragment());
    FlowControlStrategy flowControl = getFlowControlStrategyFactory().newFlowControlStrategy();
    HTTP2ServerSession session = new HTTP2ServerSession(connector.getScheduler(), endPoint, generator, listener, flowControl);
    session.setMaxLocalStreams(getMaxConcurrentStreams());
    session.setMaxRemoteStreams(getMaxConcurrentStreams());
    // For a single stream in a connection, there will be a race between
    // the stream idle timeout and the connection idle timeout. However,
    // the typical case is that the connection will be busier and the
    // stream idle timeout will expire earlier than the connection's.
    long streamIdleTimeout = getStreamIdleTimeout();
    if (streamIdleTimeout <= 0) {
        streamIdleTimeout = endPoint.getIdleTimeout();
    }
    session.setStreamIdleTimeout(streamIdleTimeout);
    session.setInitialSessionRecvWindow(getInitialSessionRecvWindow());

    ServerParser parser = newServerParser(connector, session, RateControl.NO_RATE_CONTROL);
    HTTP2Connection connection = new HTTP2ServerConnection(connector.getByteBufferPool(), executor,
            endPoint, getHttpConfiguration(), parser, session, getInputBufferSize(), listener);
    connection.addListener(connectionListener);
    return configure(connection, connector, endPoint);
}
 
Example #2
Source File: RestClient20.java    From java-11-examples with Apache License 2.0 4 votes vote down vote up
public Session createSession() throws InterruptedException, ExecutionException, TimeoutException {
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, address, new ServerSessionListener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(5, TimeUnit.SECONDS);
    return session;
}
 
Example #3
Source File: JettyClientExample.java    From http2-examples with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {
    long startTime = System.nanoTime();

    // Create and start HTTP2Client.
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();

    // Connect to host.
    String host = "localhost";
    int port = 8443;

    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);

    // Obtain the client Session object.
    Session session = sessionPromise.get(5, TimeUnit.SECONDS);

    // Prepare the HTTP request headers.
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    // Prepare the HTTP request object.
    MetaData.Request request = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    // Create the HTTP/2 HEADERS frame representing the HTTP request.
    HeadersFrame headersFrame = new HeadersFrame(request, null, true);

    // Prepare the listener to receive the HTTP response frames.
    Stream.Listener responseListener = new Stream.Listener.Adapter()
    {
        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback)
        {
            byte[] bytes = new byte[frame.getData().remaining()];
            frame.getData().get(bytes);
            int duration = (int) TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime);
            System.out.println("After " + duration + " seconds: " + new String(bytes));
            callback.succeeded();
        }
    };

    session.newStream(headersFrame, new FuturePromise<>(), responseListener);
    session.newStream(headersFrame, new FuturePromise<>(), responseListener);
    session.newStream(headersFrame, new FuturePromise<>(), responseListener);

    Thread.sleep(TimeUnit.SECONDS.toMillis(20));

    client.stop();
}