org.apache.catalina.core.JreMemoryLeakPreventionListener Java Examples

The following examples show how to use org.apache.catalina.core.JreMemoryLeakPreventionListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestVirtualContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // BZ 49218: The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
        new JreMemoryLeakPreventionListener());
}
 
Example #2
Source File: TestWarDirContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());
}
 
Example #3
Source File: TestVirtualContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // BZ 49218: The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
        new JreMemoryLeakPreventionListener());
}
 
Example #4
Source File: TestWarDirContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());
}
 
Example #5
Source File: TestVirtualContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // BZ 49218: The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
        new JreMemoryLeakPreventionListener());
}
 
Example #6
Source File: TestWarDirContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    super.setUp();

    Tomcat tomcat = getTomcatInstance();

    // The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(
            new JreMemoryLeakPreventionListener());
}
 
Example #7
Source File: Runner.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static void configureServer(Server server) {
  //server.addLifecycleListener(new SecurityListener());
  //server.addLifecycleListener(new AprLifecycleListener());
  LifecycleListener jasperListener = new JasperListener();
  server.addLifecycleListener(jasperListener);
  jasperListener.lifecycleEvent(new LifecycleEvent(server, Lifecycle.BEFORE_INIT_EVENT, null));
  server.addLifecycleListener(new JreMemoryLeakPreventionListener());
  //server.addLifecycleListener(new GlobalResourcesLifecycleListener());
  server.addLifecycleListener(new ThreadLocalLeakPreventionListener());
}
 
Example #8
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testThreadLocalLeak1() 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, "leakServlet1",
            "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMappingDecoded("/leak1", "leakServlet1");

    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("TesterLeakingServlet1",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1",
            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());
}
 
Example #9
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@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());
}
 
Example #10
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak1() 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, "leakServlet1",
            "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMapping("/leak1", "leakServlet1");

    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 [] 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",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet1",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1",
            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());
}
 
Example #11
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@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
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] 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",
            (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());
}
 
Example #12
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadLocalLeak1() 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, "leakServlet1",
            "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMapping("/leak1", "leakServlet1");

    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 [] 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",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet1",
            (WebappClassLoaderBase) ctx.getLoader().getClassLoader());

    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1",
            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());
}
 
Example #13
Source File: TestWebappClassLoaderThreadLocalMemoryLeak.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@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
    LogValidationFilter f = new LogValidationFilter(
            "The web application [] 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",
            (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());
}
 
Example #14
Source File: ServingLayer.java    From oryx with Apache License 2.0 4 votes vote down vote up
private static void configureServer(Server server) {
  server.addLifecycleListener(new JreMemoryLeakPreventionListener());
  server.addLifecycleListener(new ThreadLocalLeakPreventionListener());
}