2014-11-24 122 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