Search in sources :

Example 1 with ScheduledExecutorScheduler

use of org.eclipse.jetty.util.thread.ScheduledExecutorScheduler in project jetty.project by eclipse.

the class HttpClient method doStart.

@Override
protected void doStart() throws Exception {
    if (sslContextFactory != null)
        addBean(sslContextFactory);
    String name = HttpClient.class.getSimpleName() + "@" + hashCode();
    if (executor == null) {
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setName(name);
        executor = threadPool;
    }
    addBean(executor);
    if (byteBufferPool == null)
        byteBufferPool = new MappedByteBufferPool();
    addBean(byteBufferPool);
    if (scheduler == null)
        scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false);
    addBean(scheduler);
    transport.setHttpClient(this);
    addBean(transport);
    if (resolver == null)
        resolver = new SocketAddressResolver.Async(executor, scheduler, getAddressResolutionTimeout());
    addBean(resolver);
    handlers.put(new ContinueProtocolHandler());
    handlers.put(new RedirectProtocolHandler(this));
    handlers.put(new WWWAuthenticationProtocolHandler(this));
    handlers.put(new ProxyAuthenticationProtocolHandler(this));
    decoderFactories.add(new GZIPContentDecoder.Factory(byteBufferPool));
    cookieManager = newCookieManager();
    cookieStore = cookieManager.getCookieStore();
    super.doStart();
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler)

Example 2 with ScheduledExecutorScheduler

use of org.eclipse.jetty.util.thread.ScheduledExecutorScheduler in project jetty.project by eclipse.

the class ConnectHandler method doStart.

@Override
protected void doStart() throws Exception {
    if (executor == null)
        executor = getServer().getThreadPool();
    if (scheduler == null)
        addBean(scheduler = new ScheduledExecutorScheduler());
    if (bufferPool == null)
        addBean(bufferPool = new MappedByteBufferPool());
    addBean(selector = newSelectorManager());
    selector.setConnectTimeout(getConnectTimeout());
    super.doStart();
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler)

Example 3 with ScheduledExecutorScheduler

use of org.eclipse.jetty.util.thread.ScheduledExecutorScheduler in project jetty.project by eclipse.

the class SslContextFactoryReloadTest method testReloadWhileServing.

@Test
public void testReloadWhileServing() throws Exception {
    start(new EchoHandler());
    Scheduler scheduler = new ScheduledExecutorScheduler();
    scheduler.start();
    try {
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        ctx.init(null, SslContextFactory.TRUST_ALL_CERTS, null);
        SSLSocketFactory socketFactory = ctx.getSocketFactory();
        // Perform 4 reloads while connections are being served.
        AtomicInteger reloads = new AtomicInteger(4);
        long reloadPeriod = 500;
        AtomicBoolean running = new AtomicBoolean(true);
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                if (reloads.decrementAndGet() == 0) {
                    running.set(false);
                } else {
                    try {
                        sslContextFactory.reload(sslContextFactory -> {
                            if (sslContextFactory.getKeyStorePath().endsWith(KEYSTORE_1))
                                sslContextFactory.setKeyStorePath(KEYSTORE_2);
                            else
                                sslContextFactory.setKeyStorePath(KEYSTORE_1);
                        });
                        scheduler.schedule(this, reloadPeriod, TimeUnit.MILLISECONDS);
                    } catch (Exception x) {
                        running.set(false);
                        reloads.set(-1);
                    }
                }
            }
        }, reloadPeriod, TimeUnit.MILLISECONDS);
        byte[] content = new byte[16 * 1024];
        while (running.get()) {
            try (SSLSocket client = (SSLSocket) socketFactory.createSocket("localhost", connector.getLocalPort())) {
                // We need to invalidate the session every time we open a new SSLSocket.
                // This is because when the client uses session resumption, it caches
                // the server certificates and then checks that it is the same during
                // a new TLS handshake. If the SslContextFactory is reloaded during the
                // TLS handshake, the client will see the new certificate and blow up.
                // Note that browsers can handle this case better: they will just not
                // use session resumption and fallback to the normal TLS handshake.
                client.getSession().invalidate();
                String request1 = "" + "POST / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "\r\n";
                OutputStream outputStream = client.getOutputStream();
                outputStream.write(request1.getBytes(StandardCharsets.UTF_8));
                outputStream.write(content);
                outputStream.flush();
                InputStream inputStream = client.getInputStream();
                HttpTester.Response response1 = HttpTester.parseResponse(HttpTester.from(inputStream));
                Assert.assertNotNull(response1);
                Assert.assertThat(response1.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
                String request2 = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
                outputStream.write(request2.getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
                HttpTester.Response response2 = HttpTester.parseResponse(HttpTester.from(inputStream));
                Assert.assertNotNull(response2);
                Assert.assertThat(response2.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
            }
        }
        Assert.assertEquals(0, reloads.get());
    } finally {
        scheduler.stop();
    }
}
Also used : Request(org.eclipse.jetty.server.Request) HttpTester(org.eclipse.jetty.http.HttpTester) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) SSLContext(javax.net.ssl.SSLContext) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) Scheduler(org.eclipse.jetty.util.thread.Scheduler) SSLSocket(javax.net.ssl.SSLSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) HttpStatus(org.eclipse.jetty.http.HttpStatus) Server(org.eclipse.jetty.server.Server) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) StandardCharsets(java.nio.charset.StandardCharsets) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TimeUnit(java.util.concurrent.TimeUnit) HttpMethod(org.eclipse.jetty.http.HttpMethod) ServerConnector(org.eclipse.jetty.server.ServerConnector) Assert(org.junit.Assert) InputStream(java.io.InputStream) Scheduler(org.eclipse.jetty.util.thread.Scheduler) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) SSLContext(javax.net.ssl.SSLContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpTester(org.eclipse.jetty.http.HttpTester) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 4 with ScheduledExecutorScheduler

use of org.eclipse.jetty.util.thread.ScheduledExecutorScheduler in project jetty.project by eclipse.

the class HTTP2Client method doStart.

@Override
protected void doStart() throws Exception {
    if (executor == null)
        setExecutor(new QueuedThreadPool());
    if (scheduler == null)
        setScheduler(new ScheduledExecutorScheduler());
    if (bufferPool == null)
        setByteBufferPool(new MappedByteBufferPool());
    if (connectionFactory == null) {
        HTTP2ClientConnectionFactory h2 = new HTTP2ClientConnectionFactory();
        setClientConnectionFactory((endPoint, context) -> {
            ClientConnectionFactory factory = h2;
            SslContextFactory sslContextFactory = (SslContextFactory) context.get(SslClientConnectionFactory.SSL_CONTEXT_FACTORY_CONTEXT_KEY);
            if (sslContextFactory != null) {
                ALPNClientConnectionFactory alpn = new ALPNClientConnectionFactory(getExecutor(), h2, getProtocols());
                factory = new SslClientConnectionFactory(sslContextFactory, getByteBufferPool(), getExecutor(), alpn);
            }
            return factory.newConnection(endPoint, context);
        });
    }
    if (selector == null) {
        selector = newSelectorManager();
        addBean(selector);
    }
    selector.setConnectTimeout(getConnectTimeout());
    super.doStart();
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) SslClientConnectionFactory(org.eclipse.jetty.io.ssl.SslClientConnectionFactory) ALPNClientConnectionFactory(org.eclipse.jetty.alpn.client.ALPNClientConnectionFactory) ClientConnectionFactory(org.eclipse.jetty.io.ClientConnectionFactory) SslClientConnectionFactory(org.eclipse.jetty.io.ssl.SslClientConnectionFactory) ALPNClientConnectionFactory(org.eclipse.jetty.alpn.client.ALPNClientConnectionFactory)

Example 5 with ScheduledExecutorScheduler

use of org.eclipse.jetty.util.thread.ScheduledExecutorScheduler in project jetty.project by eclipse.

the class SessionHandler method doStart.

/* ------------------------------------------------------------ */
/*
     * @see org.eclipse.thread.AbstractLifeCycle#doStart()
     */
@Override
protected void doStart() throws Exception {
    //check if session management is set up, if not set up HashSessions
    final Server server = getServer();
    _context = ContextHandler.getCurrentContext();
    _loader = Thread.currentThread().getContextClassLoader();
    synchronized (server) {
        //Get a SessionDataStore and a SessionDataStore, falling back to in-memory sessions only
        if (_sessionCache == null) {
            SessionCacheFactory ssFactory = server.getBean(SessionCacheFactory.class);
            setSessionCache(ssFactory != null ? ssFactory.getSessionCache(this) : new DefaultSessionCache(this));
            SessionDataStore sds = null;
            SessionDataStoreFactory sdsFactory = server.getBean(SessionDataStoreFactory.class);
            if (sdsFactory != null)
                sds = sdsFactory.getSessionDataStore(this);
            else
                sds = new NullSessionDataStore();
            _sessionCache.setSessionDataStore(sds);
        }
        if (_sessionIdManager == null) {
            _sessionIdManager = server.getSessionIdManager();
            if (_sessionIdManager == null) {
                //create a default SessionIdManager and set it as the shared
                //SessionIdManager for the Server, being careful NOT to use
                //the webapp context's classloader, otherwise if the context
                //is stopped, the classloader is leaked.
                ClassLoader serverLoader = server.getClass().getClassLoader();
                try {
                    Thread.currentThread().setContextClassLoader(serverLoader);
                    _sessionIdManager = new DefaultSessionIdManager(server);
                    server.setSessionIdManager(_sessionIdManager);
                    server.manage(_sessionIdManager);
                    _sessionIdManager.start();
                } finally {
                    Thread.currentThread().setContextClassLoader(_loader);
                }
            }
            // server session id is never managed by this manager
            addBean(_sessionIdManager, false);
        }
        _scheduler = server.getBean(Scheduler.class);
        if (_scheduler == null) {
            _scheduler = new ScheduledExecutorScheduler();
            _ownScheduler = true;
            _scheduler.start();
        }
    }
    // Look for a session cookie name
    if (_context != null) {
        String tmp = _context.getInitParameter(__SessionCookieProperty);
        if (tmp != null)
            _sessionCookie = tmp;
        tmp = _context.getInitParameter(__SessionIdPathParameterNameProperty);
        if (tmp != null)
            setSessionIdPathParameterName(tmp);
        // set up the max session cookie age if it isn't already
        if (_maxCookieAge == -1) {
            tmp = _context.getInitParameter(__MaxAgeProperty);
            if (tmp != null)
                _maxCookieAge = Integer.parseInt(tmp.trim());
        }
        // set up the session domain if it isn't already
        if (_sessionDomain == null)
            _sessionDomain = _context.getInitParameter(__SessionDomainProperty);
        // set up the sessionPath if it isn't already
        if (_sessionPath == null)
            _sessionPath = _context.getInitParameter(__SessionPathProperty);
        tmp = _context.getInitParameter(__CheckRemoteSessionEncoding);
        if (tmp != null)
            _checkingRemoteSessionIdEncoding = Boolean.parseBoolean(tmp);
    }
    _sessionContext = new SessionContext(_sessionIdManager.getWorkerName(), _context);
    _sessionCache.initialize(_sessionContext);
    super.doStart();
}
Also used : Server(org.eclipse.jetty.server.Server) Scheduler(org.eclipse.jetty.util.thread.Scheduler) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler) HttpSessionContext(javax.servlet.http.HttpSessionContext)

Aggregations

ScheduledExecutorScheduler (org.eclipse.jetty.util.thread.ScheduledExecutorScheduler)20 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)9 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)8 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)7 Server (org.eclipse.jetty.server.Server)7 Scheduler (org.eclipse.jetty.util.thread.Scheduler)7 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 IOException (java.io.IOException)5 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)5 InstrumentedConnectionFactory (com.codahale.metrics.jetty9.InstrumentedConnectionFactory)4 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)4 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 ArrayList (java.util.ArrayList)3 ServletException (javax.servlet.ServletException)3 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HTTP2ServerConnectionFactory (org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory)2 ForwardedRequestCustomizer (org.eclipse.jetty.server.ForwardedRequestCustomizer)2 Request (org.eclipse.jetty.server.Request)2