2009-07-08 58 views
2

我對這是最佳實踐還是應該避免更加好奇。假設我有兩個EJBS,每個都使用不同類型的對象,小部件和foo。管理小部件的EJB需要獲取一組foos來做一些處理。這個功能已經在FooManager bean中創建。EJB調用另一個EJB方法

正在創建WidgetManager中的方法,而FooManager方法已經創建。

這裏是我的意思的一個例子。這是一個簡單的例子。

@Stateless 
public class FooManager implements FooManagerLocal, FooManagerRemote 
{ 
    public List<Foo> getAllFoosForAWidget(widgetId) 
    { 
     //runs queries and builds foo list 
    } 

    public Boolean isWidgetCloseable(widgetId) 
    { 
    //a widget is closeable if all foos for that widget are set to "done" 

    List<Foo> foos = getallFoosForAWidget(widgetId); 
    boolean isCloseable = false; 
    //process foos and update isCloseable respectively 
    return new Boolean(boolean); 

    } 

} 

@Stateless 
public class WidgetManager implements WidgetManagerLocal, WidgetManagerRemote 
{ 
    public void closeWidgetIfFoosAreDone(widgetId) //needs to do stuff with foos 
    { 
     //generate the widget based on widgetId 
     Widget widget = find(Widget.class, widgetId) 


     //is this appropriate? 
     //beans are gathered through our own beanclient 
     FooManager fooManager = BeanClient.getBean(FooManager.class); 
     if(fooManager.isWidgetCloseable(widgetId) 
     { 
     widget.setStatus(Close); 
     save(widget); //save widget back to database 
     } 
    } 
} 

我的問題是,WidgetManager bean應該調用FooManager bean中已經創建的方法嗎?客戶端是否應該檢查哪些小部件可以關閉,然後將ID列表發送到WidgetManager?

我傾向於後一種情況,以便EJB不必擔心互相調用方法。客戶可以利用它們,只需發送一個ID列表。

回答

3

我會在WidgetManager中留下簡單的'close'方法。這樣一來,它就不會太緊張。

所以,在其他一些商業Bean中,您將注入這兩個bean並構建您的邏輯。

@Stateless 
class SomeBusinessProcess implements ISomeBusinessProcess 
{ 
    @EJB FooManagerLocal fooManager; 
    @EJB WidgetManagerLocal widgetManager; 

    public void process(WidgetId id) 
    { 
     if (fooManager.isClosable(id)) 
      widgetManager.close(id); 
    } 
} 

通過這種方式,您將擁有更多的自由,並且您的Manager Bean將更加清晰。