Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several instances of a @Singleton OpenEJB bean present

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 @Singletons?

like image 981
alex440 Avatar asked Nov 08 '22 19:11

alex440


1 Answers

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

like image 124
Yan.F Avatar answered Nov 15 '22 06:11

Yan.F