Search in sources :

Example 6 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
    LogValidationFilter f = new LogValidationFilter("The web application [ROOT] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger("org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);
    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter", (WebappClassLoader) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder", (WebappClassLoader) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2", (WebappClassLoader) 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) 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 7 with ThreadPoolExecutor

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

the class StandardThreadExecutor method startInternal.

/**
     * Start the component and implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
@Override
protected void startInternal() throws LifecycleException {
    taskqueue = new TaskQueue(maxQueueSize);
    TaskThreadFactory tf = new TaskThreadFactory(namePrefix, daemon, getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS, taskqueue, tf);
    executor.setThreadRenewalDelay(threadRenewalDelay);
    if (prestartminSpareThreads) {
        executor.prestartAllCoreThreads();
    }
    taskqueue.setParent(executor);
    setState(LifecycleState.STARTING);
}
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)7 Executor (java.util.concurrent.Executor)4 TaskQueue (org.apache.tomcat.util.threads.TaskQueue)3 Context (org.apache.catalina.Context)2 JreMemoryLeakPreventionListener (org.apache.catalina.core.JreMemoryLeakPreventionListener)2 StandardHost (org.apache.catalina.core.StandardHost)2 Tomcat (org.apache.catalina.startup.Tomcat)2 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 TaskThreadFactory (org.apache.tomcat.util.threads.TaskThreadFactory)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Engine (org.apache.catalina.Engine)1 Service (org.apache.catalina.Service)1 Connector (org.apache.catalina.connector.Connector)1 ProtocolHandler (org.apache.coyote.ProtocolHandler)1 ResizableExecutor (org.apache.tomcat.util.threads.ResizableExecutor)1