1

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

  }

我究竟做错了什么?

4

1 回答 1

1

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

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

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

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

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();

另请参阅这个 SO question,您可能需要阅读更多文档(根据我的经验,理解整个 JMX 事物并使其正常工作需要一段时间)。不过,我还没有尝试将它与单元测试结合使用,所以 YMMV。

于 2016-07-12T22:58:55.297 回答