Search in sources :

Example 16 with ThreadPoolExecutor

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

the class AbstractEndpoint method setMinSpareThreads.

public void setMinSpareThreads(int minSpareThreads) {
    this.minSpareThreads = minSpareThreads;
    Executor executor = this.executor;
    if (internalExecutor && executor instanceof ThreadPoolExecutor) {
        // The internal executor should always be an instance of
        // org.apache.tomcat.util.threads.ThreadPoolExecutor but it may be
        // null if the endpoint is not running.
        // This check also avoids various threading issues.
        ((ThreadPoolExecutor) executor).setCorePoolSize(minSpareThreads);
    }
}
Also used : ResizableExecutor(org.apache.tomcat.util.threads.ResizableExecutor) Executor(java.util.concurrent.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor)

Example 17 with ThreadPoolExecutor

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

the class AbstractEndpoint method shutdownExecutor.

public void shutdownExecutor() {
    Executor executor = this.executor;
    if (executor != null && internalExecutor) {
        this.executor = null;
        if (executor instanceof ThreadPoolExecutor) {
            // this is our internal one, so we need to shut it down
            ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor;
            tpe.shutdownNow();
            long timeout = getExecutorTerminationTimeoutMillis();
            if (timeout > 0) {
                try {
                    tpe.awaitTermination(timeout, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                // Ignore
                }
                if (tpe.isTerminating()) {
                    getLog().warn(sm.getString("endpoint.warn.executorShutdown", getName()));
                }
            }
            TaskQueue queue = (TaskQueue) tpe.getQueue();
            queue.setParent(null);
        }
    }
}
Also used : ResizableExecutor(org.apache.tomcat.util.threads.ResizableExecutor) Executor(java.util.concurrent.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TaskQueue(org.apache.tomcat.util.threads.TaskQueue) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor)

Example 18 with ThreadPoolExecutor

use of org.apache.tomcat.util.threads.ThreadPoolExecutor in project tomcat 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.addServletMappingDecoded("/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 [ROOT] 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)

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