Search in sources :

Example 6 with MultiException

use of org.eclipse.jetty.util.MultiException in project jetty.project by eclipse.

the class Server method doStart.

/* ------------------------------------------------------------ */
@Override
protected void doStart() throws Exception {
    // Create an error handler if there is none
    if (_errorHandler == null)
        _errorHandler = getBean(ErrorHandler.class);
    if (_errorHandler == null)
        setErrorHandler(new ErrorHandler());
    if (_errorHandler instanceof ErrorHandler.ErrorPageMapper)
        LOG.warn("ErrorPageMapper not supported for Server level Error Handling");
    //with the shutdown handler thread.
    if (getStopAtShutdown())
        ShutdownThread.register(this);
    //Register the Server with the handler thread for receiving
    //remote stop commands
    ShutdownMonitor.register(this);
    //Start a thread waiting to receive "stop" commands.
    // initialize
    ShutdownMonitor.getInstance().start();
    LOG.info("jetty-" + getVersion());
    if (!Jetty.STABLE) {
        LOG.warn("THIS IS NOT A STABLE RELEASE! DO NOT USE IN PRODUCTION!");
        LOG.warn("Download a stable release from http://download.eclipse.org/jetty/");
    }
    HttpGenerator.setJettyVersion(HttpConfiguration.SERVER_VERSION);
    // Check that the thread pool size is enough.
    SizedThreadPool pool = getBean(SizedThreadPool.class);
    int max = pool == null ? -1 : pool.getMaxThreads();
    int selectors = 0;
    int acceptors = 0;
    for (Connector connector : _connectors) {
        if (connector instanceof AbstractConnector) {
            AbstractConnector abstractConnector = (AbstractConnector) connector;
            Executor connectorExecutor = connector.getExecutor();
            if (connectorExecutor != pool) {
                // the server level, because the connector uses a dedicated executor.
                continue;
            }
            acceptors += abstractConnector.getAcceptors();
            if (connector instanceof ServerConnector) {
                // The SelectorManager uses 2 threads for each selector,
                // one for the normal and one for the low priority strategies.
                selectors += 2 * ((ServerConnector) connector).getSelectorManager().getSelectorCount();
            }
        }
    }
    int needed = 1 + selectors + acceptors;
    if (max > 0 && needed > max)
        throw new IllegalStateException(String.format("Insufficient threads: max=%d < needed(acceptors=%d + selectors=%d + request=1)", max, acceptors, selectors));
    MultiException mex = new MultiException();
    try {
        super.doStart();
    } catch (Throwable e) {
        mex.add(e);
    }
    // start connectors last
    for (Connector connector : _connectors) {
        try {
            connector.start();
        } catch (Throwable e) {
            mex.add(e);
        }
    }
    if (isDumpAfterStart())
        dumpStdErr();
    mex.ifExceptionThrow();
    LOG.info(String.format("Started @%dms", Uptime.getUptime()));
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Executor(java.util.concurrent.Executor) MultiException(org.eclipse.jetty.util.MultiException) SizedThreadPool(org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool)

Example 7 with MultiException

use of org.eclipse.jetty.util.MultiException in project jetty.project by eclipse.

the class ContextHandler method stopContext.

/* ------------------------------------------------------------ */
protected void stopContext() throws Exception {
    //stop all the handler hierarchy
    super.doStop();
    //Call the context listeners
    ServletContextEvent event = new ServletContextEvent(_scontext);
    Collections.reverse(_destroySerletContextListeners);
    MultiException ex = new MultiException();
    for (ServletContextListener listener : _destroySerletContextListeners) {
        try {
            callContextDestroyed(listener, event);
        } catch (Exception x) {
            ex.add(x);
        }
    }
    ex.ifExceptionThrow();
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) MultiException(org.eclipse.jetty.util.MultiException) ServletContextEvent(javax.servlet.ServletContextEvent) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 8 with MultiException

use of org.eclipse.jetty.util.MultiException in project jetty.project by eclipse.

the class Server method doStop.

/* ------------------------------------------------------------ */
@Override
protected void doStop() throws Exception {
    if (isDumpBeforeStop())
        dumpStdErr();
    if (LOG.isDebugEnabled())
        LOG.debug("doStop {}", this);
    MultiException mex = new MultiException();
    // list if graceful futures
    List<Future<Void>> futures = new ArrayList<>();
    // First close the network connectors to stop accepting new connections
    for (Connector connector : _connectors) futures.add(connector.shutdown());
    // Then tell the contexts that we are shutting down
    Handler[] gracefuls = getChildHandlersByClass(Graceful.class);
    for (Handler graceful : gracefuls) futures.add(((Graceful) graceful).shutdown());
    // Shall we gracefully wait for zero connections?
    long stopTimeout = getStopTimeout();
    if (stopTimeout > 0) {
        long stop_by = System.currentTimeMillis() + stopTimeout;
        if (LOG.isDebugEnabled())
            LOG.debug("Graceful shutdown {} by ", this, new Date(stop_by));
        // Wait for shutdowns
        for (Future<Void> future : futures) {
            try {
                if (!future.isDone())
                    future.get(Math.max(1L, stop_by - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                mex.add(e);
            }
        }
    }
    // Cancel any shutdowns not done
    for (Future<Void> future : futures) if (!future.isDone())
        future.cancel(true);
    // Now stop the connectors (this will close existing connections)
    for (Connector connector : _connectors) {
        try {
            connector.stop();
        } catch (Throwable e) {
            mex.add(e);
        }
    }
    // And finally stop everything else
    try {
        super.doStop();
    } catch (Throwable e) {
        mex.add(e);
    }
    if (getStopAtShutdown())
        ShutdownThread.deregister(this);
    //Unregister the Server with the handler thread for receiving
    //remote stop commands as we are stopped already
    ShutdownMonitor.deregister(this);
    mex.ifExceptionThrow();
}
Also used : Graceful(org.eclipse.jetty.util.component.Graceful) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Date(java.util.Date) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException) Future(java.util.concurrent.Future) MultiException(org.eclipse.jetty.util.MultiException)

Example 9 with MultiException

use of org.eclipse.jetty.util.MultiException in project jetty.project by eclipse.

the class ServletHolderTest method testNoClassName.

@Test
public void testNoClassName() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class, ContextHandler.class, ServletContextHandler.class)) {
        ServletContextHandler context = new ServletContextHandler();
        ServletHandler handler = context.getServletHandler();
        ServletHolder holder = new ServletHolder();
        holder.setName("foo");
        holder.setForcedPath("/blah/");
        handler.addServlet(holder);
        handler.start();
        Assert.fail("No class in ServletHolder");
    } catch (UnavailableException e) {
        Assert.assertTrue(e.getMessage().contains("foo"));
    } catch (MultiException e) {
        MultiException m = (MultiException) e;
        Assert.assertTrue(m.getCause().getMessage().contains("foo"));
    }
}
Also used : UnavailableException(javax.servlet.UnavailableException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) MultiException(org.eclipse.jetty.util.MultiException) Test(org.junit.Test)

Example 10 with MultiException

use of org.eclipse.jetty.util.MultiException in project jetty.project by eclipse.

the class ServletHolderTest method testUnloadableClassName.

@Test
public void testUnloadableClassName() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(BaseHolder.class, ServletHandler.class, ContextHandler.class, ServletContextHandler.class)) {
        ServletContextHandler context = new ServletContextHandler();
        ServletHandler handler = context.getServletHandler();
        ServletHolder holder = new ServletHolder();
        holder.setName("foo");
        holder.setClassName("a.b.c.class");
        handler.addServlet(holder);
        handler.start();
        Assert.fail("Unloadable class");
    } catch (UnavailableException e) {
        Assert.assertTrue(e.getMessage().contains("foo"));
    } catch (MultiException e) {
        MultiException m = (MultiException) e;
        Assert.assertTrue(m.getCause().getMessage().contains("foo"));
    }
}
Also used : UnavailableException(javax.servlet.UnavailableException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) MultiException(org.eclipse.jetty.util.MultiException) Test(org.junit.Test)

Aggregations

MultiException (org.eclipse.jetty.util.MultiException)18 IOException (java.io.IOException)14 ServletException (javax.servlet.ServletException)7 FileNotFoundException (java.io.FileNotFoundException)4 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 HadoopIllegalArgumentException (org.apache.hadoop.HadoopIllegalArgumentException)4 InputStream (java.io.InputStream)3 MalformedURLException (java.net.MalformedURLException)3 JarInputStream (java.util.jar.JarInputStream)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 JarEntry (java.util.jar.JarEntry)2 UnavailableException (javax.servlet.UnavailableException)2 Handler (org.eclipse.jetty.server.Handler)2 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)2 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)2 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)2