I have also a problem with dependencies libraries that contains a classic singleton like this:
public class SingletonObject {
private static SingletonObject instance;
private SingletonObject() {}
public static synchronized SingletonObject getInstance() {
if(instance == null) {
instance = new SingletonObject();
System.out.println("SingletonObject created : " + instance);
}
return instance;
}
public void doSomething() {
System.out.println("SingletonObject doSomething . Instance=" + instance);
}
}
If this class "SingletonObject" is inside the projet Quarkus, static attribute "instance" is reset to NULL when Quarkus restarts the app. So it's good.
But if you put it this class in a
dependency, "instance" stays set, and this result to unexpected behaviour on other dependencies that use it.
I reproduced the issue on the "getting started" app:
- I created a library
org.acme.test.libx
and put the class
SingletonObject
-
I created and put
SingletonObjecInternal next to
GreetingResource.java
@Path("/hello")
public class GreetingResource {
@Inject
Logger logger;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
SingletonObject.getInstance().doSomething(); //-> in a dependency lib
SingletonObjecInternal.getInstance().doSomething(); //-> next to
GreetingResource, in same package
return "Hello RESTEasy";
}
}
On first call to "/hello", I see the correct output:
SingletonObject created: org.acme.test.libx.SingletonObject@3f0cf00
SingletonObject doSomething. Instance=org.acme.test.libx.SingletonObject@3f0cf00
SingletonObjecInternal
created: org.acme.getting.started.
SingletonObjecInternal@2dd5b5f7
SingletonObjecInternal
doSomething. Instance=org.acme.getting.started
SingletonObjecInternal@2dd5b5f7
I modified the class to make sure Quarkus restarts the application, then re-call "/hello", and I see:
SingletonObject doSomething. Instance=org.acme.test.libx.SingletonObject@3f0cf00
SingletonObjecInternal
created: org.acme.getting.started.
SingletonObjecInternal@a32c824
SingletonObjecInternal
doSomething. Instance=org.acme.getting.started
SingletonObjecInternal@a32c824
==> The static instance of
SingletonObject (external dependency) is not reset, we don't see "SingletonObject created".
Maybe it needs an other question thread?
Ps: I tried in 1.13.0.Final.
Thanks a lot for your help.