Reputation: 1657
I have a class which looks like this:
@Singleton
public class MySingletonImpl implements MySingleton{
@Override
public void init(){
...
}
@Override
public void test(){
...
}
}
It is called from the ApplicationComposer
testng
test which looks like this:
@Listeners(ApplicationComposerListener.class)
public class MyTest{
@EJB
MySingleton mySingleton;
@Module
@Classes(cdi=true, value={MySingletonImpl.class})
public EjbModule ejbModule() throws Exception{
return new EjbModule(new EjbJar());
}
@BeforeClass
public void setup(){
mySingleton.init();
}
@Test
public void test(){
mySigleton.test();
}
}
The problem I observe when the test runs, is that the object id
of the instance of MySingletonImpl
class that the test()
method called on is not the same as the instance on which the init()
method is called.
The behavior seems strange.
First, how my problem can be fixed? I want to init and then call methods on the same object, not different instances of the same class.
Second, why at all would the container instantiate multiple @Singleton
s?
Upvotes: 1
Views: 163
Reputation: 640
Is your interface is @Remote or local? If its @Local change it to @Remote:
"EJB specification doesn’t guarantee any behaviour when you use the local business interfaces for inter-modules communication even if those modules are part of the same Application Server / JVM."
https://coderanch.com/mobile/t/608212/java/instances-Singleton-EJB
Upvotes: 1