Search in sources :

Example 1 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project loc-framework by lord-of-code.

the class TomcatGracefulShutdown method onApplicationEvent.

@Override
public void onApplicationEvent(final ContextClosedEvent event) {
    LocalDateTime startShutdown = LocalDateTime.now();
    LocalDateTime stopShutdown = LocalDateTime.now();
    try {
        log.info("We are now in down mode, please wait " + tomcatGracefulShutdownProperties.getWaitTime() + " second(s)...");
        if (connector == null) {
            log.info("We are running unit test ... ");
            return;
        }
        connector.pause();
        final Executor executor = connector.getProtocolHandler().getExecutor();
        if (executor instanceof ThreadPoolExecutor) {
            log.info("executor is  ThreadPoolExecutor");
            final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
            threadPoolExecutor.shutdown();
            if (!threadPoolExecutor.awaitTermination(tomcatGracefulShutdownProperties.getWaitTime(), TimeUnit.SECONDS)) {
                log.warn("Tomcat thread pool did not shut down gracefully within " + tomcatGracefulShutdownProperties.getWaitTime() + " second(s). Proceeding with force shutdown");
            } else {
                log.debug("Tomcat thread pool is empty, we stop now");
            }
        }
        stopShutdown = LocalDateTime.now();
    } catch (final InterruptedException ex) {
        log.error("The await termination has been interrupted : " + ex.getMessage());
        Thread.currentThread().interrupt();
    } finally {
        final long seconds = Duration.between(startShutdown, stopShutdown).getSeconds();
        log.info("Shutdown performed in " + seconds + " second(s)");
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor)

Example 2 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project tomcat70 by apache.

the class ThreadLocalLeakPreventionListener method stopIdleThreads.

/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 *
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping)
        return;
    if (!(context instanceof StandardContext) || !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. " + "It is not configured to do it.");
        return;
    }
    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }
            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor = (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }
        }
    }
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) Connector(org.apache.catalina.connector.Connector) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) Service(org.apache.catalina.Service) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) Engine(org.apache.catalina.Engine)

Example 3 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project tomcat70 by apache.

the class AsyncChannelGroupUtil method createAsynchronousChannelGroup.

private static AsynchronousChannelGroup createAsynchronousChannelGroup() {
    // Need to do this with the right thread context class loader else the
    // first web app to call this will trigger a leak
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(AsyncIOThreadFactory.class.getClassLoader());
        // These are the same settings as the default
        // AsynchronousChannelGroup
        int initialSize = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, Long.MAX_VALUE, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new AsyncIOThreadFactory());
        try {
            return AsynchronousChannelGroup.withCachedThreadPool(executorService, initialSize);
        } catch (IOException e) {
            // No good reason for this to happen.
            throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail"));
        }
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) IOException(java.io.IOException)

Example 4 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project tomcat70 by apache.

the class TestWebappClassLoaderThreadLocalMemoryLeak method testThreadLocalLeak2.

@Test
public void testThreadLocalLeak2() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "leakServlet2", "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMapping("/leak2", "leakServlet2");
    tomcat.start();
    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);
    // Configure logging filter to check leak message appears
    TesterLogValidationFilter f = TesterLogValidationFilter.add(null, "The web application [] created a ThreadLocal with key of", null, "org.apache.catalina.loader.WebappClassLoaderBase");
    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2", (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2", new ByteChunk(), null);
    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;
    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost()).findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);
    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TesterLogValidationFilter(org.apache.tomcat.unittest.TesterLogValidationFilter) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardHost(org.apache.catalina.core.StandardHost) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project tomcat70 by apache.

the class AbstractEndpoint method createExecutor.

public void createExecutor() {
    internalExecutor = true;
    TaskQueue taskqueue = new TaskQueue();
    TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS, taskqueue, tf);
    taskqueue.setParent((ThreadPoolExecutor) executor);
}
Also used : TaskQueue(org.apache.tomcat.util.threads.TaskQueue) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TaskThreadFactory(org.apache.tomcat.util.threads.TaskThreadFactory)

Aggregations

ThreadPoolExecutor (org.apache.tomcat.util.threads.ThreadPoolExecutor)18 Executor (java.util.concurrent.Executor)10 TaskQueue (org.apache.tomcat.util.threads.TaskQueue)6 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)4 Context (org.apache.catalina.Context)4 JreMemoryLeakPreventionListener (org.apache.catalina.core.JreMemoryLeakPreventionListener)4 StandardHost (org.apache.catalina.core.StandardHost)4 Tomcat (org.apache.catalina.startup.Tomcat)4 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)4 TesterLogValidationFilter (org.apache.tomcat.unittest.TesterLogValidationFilter)4 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)4 TaskThreadFactory (org.apache.tomcat.util.threads.TaskThreadFactory)4 Test (org.junit.Test)4 ResizableExecutor (org.apache.tomcat.util.threads.ResizableExecutor)3 IOException (java.io.IOException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Engine (org.apache.catalina.Engine)2 Service (org.apache.catalina.Service)2 Connector (org.apache.catalina.connector.Connector)2 ProtocolHandler (org.apache.coyote.ProtocolHandler)2