I have situation where I share singleton between my code which runs the embedded-server and my web-application. I have war with classes and deployment tool. When I printf instances I see:
abc.Abc@173a10f
abc.Abc@105738
So this is not really singleton. How this works?
My server Jetty start code:
public static void main(String[] args) throws Exception
{
System.out.println(MySingleton.getInstance());
// start Jetty here and deploy war with WebAppContext()
}
My ServletContextListener
side code:
public class AppServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(MySingleton.getInstance());
}
}
My singleton:
public class MySingleton {
private static MySingleton INSTANCE = new MySingleton();
private MySingleton () {}
public static MySingleton getInstance() {
return INSTANCE;
}
}
I forced exception inside constructor. It looks like I get two different.
java.lang.Exception
at api.MySingleton.<init>(MySingleton.java:33)
at api.MySingleton.<clinit>(MySingleton.java:22)
at my.project.StartJetty.main(StartJetty.java:41)
java.lang.Exception
at api.MySingleton.<init>(MySingleton.java:33)
at api.MySingleton.<clinit>(MySingleton.java:22)
at api.AppServletContextListener.contextInitialized(AppServletContextListener.java:25)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:640)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:229)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1208)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:586)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:449)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
at org.eclipse.jetty.server.Server.doStart(Server.java:258)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
at my.project.StartJetty.main(StartJetty.java:66)
Your ServletContextListener and main classes are loaded by different class loaders. Whenever a class gets loaded, its static block will be executed. so you will get two instances...
check this link for more details http://www.javaworld.com/article/2073352/core-java/simply-singleton.html?page=2
Have a look at some Jetty documentation. You can play around with class loading configurations.
This is exactly the situation you are describing. One
MySingleton
instance is being loaded by the main Java program and another is being loaded by Jetty's class loader.Printing a stack trace in the constructor should give you the information you need to find out where it is being instantiated and what order. The Singleton pattern is a dangerous thing, usually better to do it a different way.