2014-11-24 127 views
0

我是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我不知道我是否應該在這裏使用,但是也沒有運氣。

請大家幫忙。提前致謝。

回答

0

我已經得到了答案。 A類可以用「Singleton」註釋,而B類(無狀態)可以很好地訪問它的方法,沒有任何問題。您不必爲類A提供任何視圖。默認情況下,它將是無界面視圖。以下是幫助我理解的鏈接。

EJB 3.1 @LocalBean vs no annotation