I am new to EJB3.1 .Please bear with me if this is a trivial question. My requirement is to have a singleton class which will have certain data shared across different beans. And to have different Threads accessing this singleton class's data . Let me try to explain using two different class called A and B .
@Singleton
//@Local?? ,@Remote?? or @LocalBean ?
class A {
private List<CommonDTO> commonDTOList = new ArrayList<CommonDTO>();
.
. //other member variables, EJB beans which implement Remote interfaces.
.
init(){
//initialise commonDTOList here.
}
//getter
List<SomeDTO> getCommonDTOList(){
return commonDTOList;
}
}
@Stateless
Class B implements Interface { //Interface is @Remote
//need to access singleton Class A's getter , so that all the threads have the same commonDTOList.
@EJB
private A classA;
.
.//other member variables
.
@OverRide //overriding Interface2's method
public void doSomething(){
.
.//do some database transactions here , which can be done parallely by multiple threads, since this is stateless.
.
//now retrieve Class A'S CommonDTOList.
//This List should be shared across multiple threads of this stateless bean.
List<SomeDTO> someDTOListInsideStatelessBean = classA.getCommonDTOList();
}
}
Question is what annotation I should on ClassA
so that I can access its List in another Stateless bean.?
I have tried the following but in vain.
1) @Local
I cannot use because like mentioned in the inline comments above .In classA
that member variables has @EJB
beans which implement @Remote
interfaces.
2) @LocalBean
looks to be the one used here in this scenario. However , once inside the method "doSomething()
" of ClassB
, the classA
variable has all its
member variable as null .Although the List was initialised during start up.
I was under the impression that since its singleton , there will be only instance shared
across all beans.
3)@Remote
I am not sure if I should be using here , however no luck with that too.
please help. Thanks in advance.
I have got the answer. The Class A can be annotated with just "Singleton" and Class B(Stateless) can access its methods just fine without any issue.You dont have to provide any view for Class A. By default it will be no-interface view. Below is the link which helped me understand.
EJB 3.1 @LocalBean vs no annotation