hello

________________________________
From: oren peer <oren1...@gmail.com>
Sent: Saturday, July 23, 2016 2:30 AM
To: users@tomcat.apache.org
Subject: Tomcat 8.0.33, Ubuntu 14.04.3 : shared classloader with embedded 
Tomcat 8

I have upgraded tomcat from version 7.0.34 to version 8.0.33, and since
then I have been facing a problem to share the web application context and
Junit context.

I have a web application with singleton class that gathers statistic data
about the web application. I also have Junit that runs the web application
in embedded tomcat. the Junit queries the web application and then checks
the statistic data.

I try to make a simple example:

the singleton:

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

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

Test unit:

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

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe",
System.getProperty("FEBaseDir"));

   tomcat.start();

   Counter.getInstance().getValue();

  }

when I used Tomcat 7, everything worked fine. but since I upgraded
tomcat to tomcat 8.0.33, It hasn't been working. the singleton class
with the static data loads twice. first by the tomcat and then by the
Junit itself.

I have tried to pass tomcat a classloader but it doesn't work.

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

   tomcat.setPort(50000);
   StandardContext ctx = (StandardContext) tomcat.addWebapp("/fe",
System.getProperty("FEBaseDir")); /

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

  }


What am I doing wrong?

Thanks!

Oren

Reply via email to