嵌入Tomcat 8的共享类加载器 [英] shared classloader with embedded Tomcat 8

查看:0
本文介绍了嵌入Tomcat 8的共享类加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有一个带有Singleton类的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();
  }

}

Servlet:

@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");
  }
}

contextListener:

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

  }

当我使用Tomcat7时,一切都运行正常。但是自从我把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();

  }

我做错了什么?

推荐答案

您可以尝试使用StandardContext中的setDelegate方法来阻止Web应用程序类加载器重新加载Counter类,但这会以一种不好的方式影响安全性,因此我建议不要这样做。
公开统计信息的常用方法是使用JMX(MBean)。您可以通过使用值true调用StandardContext中的setUseNaming方法来启用此功能。

您可以这样注册一个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。

这篇关于嵌入Tomcat 8的共享类加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆