【问题标题】:shared classloader with embedded Tomcat 8与嵌入式 Tomcat 8 共享类加载器
【发布时间】:2016-11-15 04:16:59
【问题描述】:

我已将 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();

  }

我做错了什么?

【问题讨论】:

    标签: java tomcat classloader tomcat8 embedded-tomcat-8


    【解决方案1】:

    您可以尝试在StandardContext 中使用setDelegate 方法来防止web-app 类加载器重新加载Counter 类,但这会以不好的方式影响安全性,所以我建议不要这样做。
    公开统计信息的常用方法是使用 JMX (MBean)。您可以通过调用StandardContext 中的setUseNaming 方法来启用此功能,其值为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。

    【讨论】:

      猜你喜欢
      • 2011-03-05
      • 2017-01-07
      • 2022-06-17
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多