CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-eclipse-jetty--jetty-server

Core server component of Eclipse Jetty web server providing HTTP server functionality, request handling, and connection management

Pending
Overview
Eval results
Files

connection-management.mddocs/

Connection Management

Connection management in Jetty handles network connectivity, protocol negotiation, SSL/TLS support, and connection lifecycle management through connectors and connection factories.

Connection Factories

Connection factories create and configure protocol-specific connections.

ConnectionFactory Interface

public interface ConnectionFactory {
    // Protocol identification
    String getProtocol();
    List<String> getProtocols();
    
    // Connection creation
    Connection newConnection(Connector connector, EndPoint endPoint);
    
    // Nested interfaces
    interface Detecting extends ConnectionFactory {
        Detection detect(ByteBuffer buffer);
    }
    
    interface Configuring extends ConnectionFactory {
        EndPoint configure(EndPoint endPoint, Connector connector, ConnectionMetaData connectionMetaData);
    }
}

AbstractConnectionFactory

Base implementation for connection factories.

public abstract class AbstractConnectionFactory implements ConnectionFactory {
    // Constructors
    protected AbstractConnectionFactory(String protocol);
    protected AbstractConnectionFactory(String... protocols);
    
    // Protocol management
    public String getProtocol();
    public List<String> getProtocols();
    
    // Template methods
    public abstract Connection newConnection(Connector connector, EndPoint endPoint);
    
    // Utility methods
    protected void configure(Connection connection, Connector connector, EndPoint endPoint);
}

HTTP Connection Factories

HttpConnectionFactory

Factory for HTTP/1.1 connections.

public class HttpConnectionFactory extends AbstractConnectionFactory 
        implements HttpConfiguration.ConnectionFactory {
    
    // Constructors
    public HttpConnectionFactory();
    public HttpConnectionFactory(HttpConfiguration config);
    public HttpConnectionFactory(HttpConfiguration config, String protocol);
    
    // Configuration
    public HttpConfiguration getHttpConfiguration();
    public void setHttpConfiguration(HttpConfiguration config);
    
    // Connection creation
    public Connection newConnection(Connector connector, EndPoint endPoint);
}

Usage Example

// Create HTTP configuration
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(8443);
httpConfig.setOutputBufferSize(32768);
httpConfig.setRequestHeaderSize(8192);
httpConfig.setResponseHeaderSize(8192);

// Create HTTP connection factory
HttpConnectionFactory httpFactory = new HttpConnectionFactory(httpConfig);

// Create connector with HTTP factory
ServerConnector httpConnector = new ServerConnector(server, httpFactory);
httpConnector.setPort(8080);
server.addConnector(httpConnector);

SSL/TLS Connection Factory

SslConnectionFactory

Factory for SSL/TLS encrypted connections.

public class SslConnectionFactory extends AbstractConnectionFactory 
        implements ConnectionFactory.Detecting, ConnectionFactory.Configuring {
    
    // Constructors
    public SslConnectionFactory();
    public SslConnectionFactory(String nextProtocol);
    public SslConnectionFactory(SslContextFactory.Server sslContextFactory, String nextProtocol);
    
    // SSL configuration
    public SslContextFactory.Server getSslContextFactory();
    public void setSslContextFactory(SslContextFactory.Server sslContextFactory);
    
    // Protocol negotiation
    public String getNextProtocol();
    public void setNextProtocol(String nextProtocol);
    
    // ALPN support
    public boolean isDirectBuffersForEncryption();
    public void setDirectBuffersForEncryption(boolean useDirectBuffers);
    
    // Connection creation
    public Connection newConnection(Connector connector, EndPoint endPoint);
    public EndPoint configure(EndPoint endPoint, Connector connector, 
                            ConnectionMetaData connectionMetaData);
}

SSL Configuration Example

// Create SSL context factory
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath("keystore.jks");
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.setKeyManagerPassword("password");
sslContextFactory.setTrustStorePath("truststore.jks");
sslContextFactory.setTrustStorePassword("password");

// Enable client certificate authentication (optional)
sslContextFactory.setWantClientAuth(true);
sslContextFactory.setNeedClientAuth(false);

// Configure cipher suites
sslContextFactory.setIncludeCipherSuites("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384");
sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA");

// Configure protocols
sslContextFactory.setIncludeProtocols("TLSv1.2", "TLSv1.3");
sslContextFactory.setExcludeProtocols("SSLv2", "SSLv3", "TLSv1", "TLSv1.1");

// Create HTTP configuration for HTTPS
HttpConfiguration httpsConfig = new HttpConfiguration();
httpsConfig.setSecureScheme("https");
httpsConfig.setSecurePort(8443);
httpsConfig.addCustomizer(new SecureRequestCustomizer());

// Create HTTPS connector
ServerConnector httpsConnector = new ServerConnector(server,
    new SslConnectionFactory(sslContextFactory, "http/1.1"),
    new HttpConnectionFactory(httpsConfig));
httpsConnector.setPort(8443);
server.addConnector(httpsConnector);

Protocol Detection

DetectorConnectionFactory

Factory that detects protocols based on connection data.

public class DetectorConnectionFactory extends AbstractConnectionFactory 
        implements ConnectionFactory.Detecting {
    
    // Constructor
    public DetectorConnectionFactory(ConnectionFactory... factories);
    
    // Detection configuration
    public void addDetectingConnectionFactory(ConnectionFactory.Detecting factory);
    public List<ConnectionFactory.Detecting> getDetectingConnectionFactories();
    
    // Connection creation
    public Connection newConnection(Connector connector, EndPoint endPoint);
    public Detection detect(ByteBuffer buffer);
}

Usage Example

// Create factories for different protocols
HttpConnectionFactory httpFactory = new HttpConnectionFactory();
HTTP2ServerConnectionFactory http2Factory = new HTTP2ServerConnectionFactory(httpConfig);

// Create detector that can handle both HTTP/1.1 and HTTP/2
DetectorConnectionFactory detector = new DetectorConnectionFactory(
    httpFactory, http2Factory);

// Create connector with detection
ServerConnector connector = new ServerConnector(server, detector);
connector.setPort(8080);

Connection Limiting and Control

ConnectionLimit

Controls the maximum number of concurrent connections.

public class ConnectionLimit extends AbstractLifeCycle 
        implements Connection.Listener, SelectorManager.AcceptListener {
    
    // Constructor
    public ConnectionLimit(int maxConnections, Connector... connectors);
    
    // Configuration
    public int getMaxConnections();
    public void setMaxConnections(int maxConnections);
    public int getConnections();
    
    // Connection tracking
    public void onOpened(Connection connection);
    public void onClosed(Connection connection);
    
    // Accept control
    public void onAccepting(SelectableChannel channel);
    public void onAcceptFailed(SelectableChannel channel, Throwable cause);
}

AcceptRateLimit

Limits the rate of accepting new connections.

public class AcceptRateLimit extends AbstractLifeCycle 
        implements SelectorManager.AcceptListener, Runnable {
    
    // Constructor
    public AcceptRateLimit(int maxRate, long period, TimeUnit units, Connector... connectors);
    
    // Configuration
    public int getMaxRate();
    public void setMaxRate(int maxRate);
    public long getPeriod();
    public void setPeriod(long period, TimeUnit units);
    
    // Rate tracking
    public void onAccepting(SelectableChannel channel);
}

Usage Example

Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);

// Limit to 1000 concurrent connections
ConnectionLimit connectionLimit = new ConnectionLimit(1000, connector);
server.addBean(connectionLimit);

// Limit to 100 connections per second
AcceptRateLimit rateLimit = new AcceptRateLimit(100, 1, TimeUnit.SECONDS, connector);
server.addBean(rateLimit);

// Monitor for low resource conditions
LowResourceMonitor lowResourceMonitor = new LowResourceMonitor(server);
lowResourceMonitor.setMonitoredConnectors(connector);
lowResourceMonitor.setMaxMemory(1024 * 1024 * 1024); // 1GB
lowResourceMonitor.setMaxConnections(800); // 80% of connection limit
server.addBean(lowResourceMonitor);

Low Resource Monitoring

LowResourceMonitor

Monitors server resources and takes action during low resource conditions.

public class LowResourceMonitor extends ContainerLifeCycle {
    
    // Constructor
    public LowResourceMonitor(Server server);
    
    // Resource thresholds
    public void setMaxMemory(long maxMemory);
    public long getMaxMemory();
    public void setMaxConnections(int maxConnections);
    public int getMaxConnections();
    public void setLowResourcesIdleTimeout(long lowResourcesIdleTimeout);
    public long getLowResourcesIdleTimeout();
    
    // Monitoring configuration
    public void setMonitoredConnectors(Connector... connectors);
    public Collection<Connector> getMonitoredConnectors();
    public void setMonitorThreads(boolean monitorThreads);
    public boolean isMonitorThreads();
    
    // Resource state
    public boolean isLowOnResources();
    public String getLowResourcesReasons();
}

Connection Metadata

ConnectionMetaData Interface

Provides information about active connections.

public interface ConnectionMetaData extends Attributes {
    // Connection identification
    String getId();
    
    // Protocol information
    String getProtocol();
    HttpConfiguration getHttpConfiguration();
    
    // Network information
    SocketAddress getLocalSocketAddress();
    SocketAddress getRemoteSocketAddress();
    
    // Security information
    boolean isSecure();
    X509Certificate[] getPeerCertificates();
}

Advanced Connection Configuration

Multiple Protocol Support

// Support HTTP/1.1 and HTTP/2 on same port
HttpConfiguration config = new HttpConfiguration();
config.setSendServerVersion(false);
config.setSendDateHeader(false);

// HTTP/1.1 factory
HttpConnectionFactory http11 = new HttpConnectionFactory(config);

// HTTP/2 factory
HTTP2ServerConnectionFactory http2 = new HTTP2ServerConnectionFactory(config);
http2.setMaxConcurrentStreams(128);
http2.setInitialStreamRecvWindow(65536);

// ALPN negotiation factory
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol("http/1.1");

// SSL factory with ALPN
SslContextFactory.Server sslContextFactory = createSslContextFactory();
SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());

// Create connector supporting HTTP/1.1 and HTTP/2 over TLS with ALPN
ServerConnector connector = new ServerConnector(server, ssl, alpn, http2, http11);
connector.setPort(8443);

Custom Connection Factory

public class CustomProtocolConnectionFactory extends AbstractConnectionFactory {
    
    public CustomProtocolConnectionFactory() {
        super("custom-protocol");
    }
    
    @Override
    public Connection newConnection(Connector connector, EndPoint endPoint) {
        return new CustomProtocolConnection(endPoint, connector.getExecutor());
    }
}

// Custom connection implementation
public class CustomProtocolConnection extends AbstractConnection {
    
    public CustomProtocolConnection(EndPoint endPoint, Executor executor) {
        super(endPoint, executor);
    }
    
    @Override
    public void onOpen() {
        super.onOpen();
        // Initialize custom protocol
        fillInterested(); // Start reading
    }
    
    @Override
    public void onFillable() {
        // Handle incoming data for custom protocol
        ByteBuffer buffer = getByteBufferPool().acquire(1024, false);
        try {
            int filled = getEndPoint().fill(buffer);
            if (filled > 0) {
                processCustomProtocolData(buffer);
            }
            if (filled < 0) {
                getEndPoint().close();
            } else {
                fillInterested(); // Continue reading
            }
        } catch (IOException e) {
            getEndPoint().close(e);
        } finally {
            getByteBufferPool().release(buffer);
        }
    }
    
    private void processCustomProtocolData(ByteBuffer buffer) {
        // Process custom protocol data
    }
}

Connection Statistics and Monitoring

Connection Listener

public class ConnectionMonitoringListener implements Connection.Listener {
    private final AtomicLong connectionsOpened = new AtomicLong();
    private final AtomicLong connectionsClosed = new AtomicLong();
    private final Map<String, Long> connectionDurations = new ConcurrentHashMap<>();
    
    @Override
    public void onOpened(Connection connection) {
        connectionsOpened.incrementAndGet();
        connectionDurations.put(connection.toString(), System.currentTimeMillis());
        System.out.println("Connection opened: " + connection);
    }
    
    @Override
    public void onClosed(Connection connection) {
        connectionsClosed.incrementAndGet();
        Long startTime = connectionDurations.remove(connection.toString());
        if (startTime != null) {
            long duration = System.currentTimeMillis() - startTime;
            System.out.println("Connection closed: " + connection + ", Duration: " + duration + "ms");
        }
    }
    
    public long getConnectionsOpened() {
        return connectionsOpened.get();
    }
    
    public long getConnectionsClosed() {
        return connectionsClosed.get();
    }
    
    public long getActiveConnections() {
        return connectionsOpened.get() - connectionsClosed.get();
    }
}

// Usage
ConnectionMonitoringListener monitor = new ConnectionMonitoringListener();
connector.addBean(monitor);

Install with Tessl CLI

npx tessl i tessl/maven-org-eclipse-jetty--jetty-server

docs

configuration.md

connection-management.md

context-resources.md

handlers.md

index.md

request-logging.md

request-response.md

security-ssl.md

server-core.md

session-management.md

utility-handlers.md

tile.json