我是EJB3.1的新手。如果這是一個微不足道的問題,請耐心等待。我的要求是擁有一個單獨的類,它將在不同的bean間共享某些數據。讓不同的線程訪問這個單例類的數據。讓我試着用兩個不同的類A和B來解釋。@ EJB3中@ Singleton's bean的無狀態bean訪問數據
@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();
}
}
的問題是什麼註解我應該ClassA
這樣我就可以在另一個無國籍豆訪問其表? 我試過以下,但徒勞無功。
1)@Local
我不能使用,因爲在上面。在classA
該成員變量具有@EJB
菜豆,實現@Remote
接口內嵌批註,提及等等。
2)@LocalBean
看起來是在這種情況下使用的。但是,一旦在ClassB
的方法「doSomething()
」內,classA
變量的所有 成員變量都爲null。雖然List在啓動期間已初始化。 我的印象是,自從它的單例以來,所有bean中將只有實例共享 。
3)@Remote
我不知道我是否應該在這裏使用,但是也沒有運氣。
請大家幫忙。提前致謝。