Skip to content

Commit

Permalink
Silence Tomcat INFO logs, disableChecksAndJarScan
Browse files Browse the repository at this point in the history
The TestTomcat worked perfectly well, but generated chatty logs. The
first step was to call `tomcat.setSilent(true)` to suppress most of
these. However, these messages remained:

  Nov 06, 2023 4:40:21 PM org.apache.jasper.servlet.TldScanner scanJars
  INFO: At least one JAR was scanned for TLDs yet contained no TLDs.
  Enable debug logging for this logger for a complete list of JARs that
  were scanned but no TLDs were found in them. Skipping unneeded JARs
  during scanning can improve startup time and JSP compilation time.

  Nov 06, 2023 4:40:21 PM
  org.apache.catalina.loader.WebappClassLoaderBase
  checkThreadLocalsForLeaks
  WARNING: You need to add "--add-opens=java.base/java.lang=ALL-UNNAMED"
  to the JVM command line arguments to enable ThreadLocal memory leak
  detection.  Alternatively, you can suppress this warning by disabling
  ThreadLocal memory leak detection.

  Nov 06, 2023 4:40:21 PM
  org.apache.catalina.loader.WebappClassLoaderBase
  clearReferencesRmiTargets
  WARNING: You need to add
  "--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED" to the JVM
  command line arguments to enable RMI Target memory leak detection.
  Alternatively, you can suppress this warning by disabling RMI Target
  memory leak detection.

I had to dig through the Tomcat source and API docs to figure out that I
needed to update the StandardContext returned from `tomcat.addWebapp()`.
Fortunately I remembered from the tutorials linked from the class
comment that this was indeed possible and OK to do.

- https://github.com/apache/tomcat
- https://tomcat.apache.org/tomcat-10.1-doc/api/index.html
  • Loading branch information
mbland committed Nov 6, 2023
1 parent d5ad001 commit 0bcb4a3
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
package com.mike_bland.training.testing.utils;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.tomcat.JarScanFilter;
import org.apache.tomcat.JarScanType;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -51,8 +54,11 @@ public synchronized void start() throws LifecycleException {
running = true;
tomcat = new Tomcat();
tomcat.setPort(port);
tomcat.setSilent(true);

final var ctx = tomcat.addWebapp(contextPath, WEB_APP_DIR);
final var ctx = (StandardContext) tomcat.addWebapp(
contextPath, WEB_APP_DIR
);
final var root = new StandardRoot(ctx);
final var resourceSet = new DirResourceSet(
root,
Expand All @@ -63,6 +69,7 @@ public synchronized void start() throws LifecycleException {

root.addPreResources(resourceSet);
ctx.setResources(root);
disableChecksAndJarScan(ctx);

// getConnector() is a recent requirement the other examples didn't use.
// - https://stackoverflow.com/questions/15114892/embedded-tomcat-without-web-inf#comment98210881_15235711
Expand All @@ -71,6 +78,22 @@ public synchronized void start() throws LifecycleException {
tomcat.start();
}

private static void disableChecksAndJarScan(StandardContext ctx) {
ctx.setClearReferencesThreadLocals(false);
ctx.setClearReferencesRmiTargets(false);
ctx.getJarScanner().setJarScanFilter(new JarScanFilter() {
@Override
public boolean check(JarScanType jarScanType, String jarName) {
return false;
}

@Override
public boolean isSkipAll() {
return true;
}
});
}

public URI uri() {
return this.uri;
}
Expand Down

0 comments on commit 0bcb4a3

Please sign in to comment.