java - 与嵌入式 Tomcat 8 共享类加载器

标签 java tomcat classloader tomcat8 embedded-tomcat-8

我已经将 tomcat 从 7.0.34 版本升级到 8.0.33 版本,从那时起,我一直面临共享 Web 应用程序上下文和 Junit 上下文的问题。

我有一个带有单例类的 Web 应用程序,用于收集有关 Web 应用程序的统计数据。我还有在嵌入式 tomcat 中运行 Web 应用程序的 Junit。 Junit 查询 Web 应用程序,然后检查统计数据。

我尝试做一个简单的例子:

单例:

  public class Counter {

  private static Counter instance;
  private AtomicLong counter;

  private Counter(){}

  public static Counter getInstance(){
    if(instance == null){
      synchronized (Counter.class) {
        if(instance == null){
          instance = new Counter();
        }
      }
    }

    return instance;
  }

  public long incrementAndGet(){
    return counter.incrementAndGet();
  }

  public long getValue(){
    return counter.get();
  }

}

小服务程序:

@WebServlet(name="servlet",loadOnStartup=1, urlPatterns="/servletTest")
public class Servlet extends HttpServlet{

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.getWriter().write("Hi, you are the #" + Counter.getInstance().incrementAndGet() + " visitor");
  }
}

上下文监听器:

public class MyContextListener implements ServletContextListener{
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {}

  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    Counter.getInstance().incrementAndGet(); 
  }
}

测试单元:

  public void mainTest() throws ServletException, LifecycleException{
    Tomcat tomcat = new Tomcat();

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile

   tomcat.start();

   Counter.getInstance().getValue();

  }

当我使用 Tomcat 7 时,一切正常。但是自从我将tomcat升级到tomcat 8.0.33后,它一直没有工作。具有静态数据的单例类加载两次。首先由 tomcat,然后由 Junit 本身。

我试图向 tomcat 传递一个类加载器,但它不起作用。

 public void mainTest() throws ServletException, LifecycleException{
    Tomcat tomcat = new Tomcat();

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe", System.getProperty("FEBaseDir")); //The FEBaseDir property is supposed to be taken from Maven build using 'test' profile

   ctx.setCrossContext(true);
   ctx.setLoader((Loader) new WebappLoader(Thread.currentThread().getContextClassLoader()));

   ctx.setParentClassLoader(Thread.currentThread().getContextClassLoader());

   tomcat.getEngine().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getHost().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getService().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.getServer().setParentClassLoader(Thread.currentThread().getContextClassLoader());
   tomcat.start();

   Counter.getInstance().getValue();

  }

我做错了什么?

最佳答案

您可以尝试使用 setDelegate StandardContext 中的方法来防止网络应用程序类加载器重新加载 Counter 类,但这会以不好的方式影响安全性,所以我建议不要这样做。
通常公开统计数据的方法是使用JMX (MBeans)。您可以通过调用 setUseNaming 来启用它StandardContext 中的方法,值为 true

您可以像这样注册一个 mbean(从 here 复制):

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName beanPoolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")");
mBeanServer.registerMBean(hikariPool, beanPoolName);

您可以检索这样的值(从 here 复制):

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (foo)");
HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);

int idleConnections = poolProxy.getIdleConnections();

另见 this SO question并且您可能需要阅读更多文档(根据我的经验,理解整个 JMX 并使其工作需要一些时间)。不过,我还没有尝试将此与单元测试结合使用,所以 YMMV。

关于java - 与嵌入式 Tomcat 8 共享类加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38337061/

相关文章:

java - AppClassloader 和 SystemClassloader 的区别

java - Android中parcelable.readArrayList()中ClassLoader的使用

java性能问题-正则表达式VS内部String方法

JavaMail 类NotFoundException

java - 如何通过 Java EE Web 应用程序将文件存储在服务器(Web 容器)上?

java - 如何修复内存泄漏问题在tomcat中发现内存泄漏的原因是什么

java - Android Studio 链接引用失败

java - 如何弄清楚 .class 文件的作用?

java.lang.ClassNotFoundException : javax. servlet.ServletContainerInitializer 异常

java - 从/bin/content 加载类而不是从 standalone/deployments