2010-08-15 59 views
4

我想在池中創建一個無狀態bean時創建一個計時器EJB3。 但是,如果使用@PostConstruct我得到異常:如何使用@PostConstruct在無狀態bean EJB3中創建定時器?

java.lang.IllegalStateException: [EJB:010193]Illegal call to EJBContext method. The bean is in "null" state. It cannot perform 'getting the Timer Service' action(s). Refer to the EJB specification for more details.

如果容器調用@PostConstruct,豆ins't空。那麼,爲什麼我得到這個異常?


CLASS

@Stateless 
public class TestBean implements TestLocal { 

    @Resource 
    TimerService timerService; 

    @PostConstruct 
    public void startTimer() { 
     if (timerService.getTimers().size() == 0) { 
      timerService.createTimer(1 * 1000, 1 * 1000, null); 
     } 
    } 

    @Override 
    public void test() {   
    } 

} 

INTERFACE

@Local 
public interface TesteLocal { 

    void test(); 

} 

SERV LET

public class TestServlet extends HttpServlet { 
    @EJB 
    private TestLocal test; 

    protected void doGet(....) throws .... { 
     test.test(); 
    } 
} 

詳情

我使用WebLogic服務器11g。

回答

-2

我不是100%確定的,但我認爲bean類必須實現javax.ejb.TimedObject或者使用註釋爲@Timeout的方法來使用EJB定時器。示例:

@Stateless 
public class TestBean implements TestLocal { 

    @Resource 
    TimerService timerService; 

    @PostConstruct 
    public void startTimer() { 
     if (timerService.getTimers().size() == 0) { 
      timerService.createTimer(1 * 1000, 1 * 1000, null); 
     } 
    } 

    @Timeout 
    @TransactionAttribute(value=REQUIRES_NEW) 
    public void timeoutCallback(Timer timer) { 
     ... 
    } 

} 

WebLogic是否還抱怨上面的代碼? PS:在任何情況下,您目前收到的錯誤報告都很差,您應該打開一個案例。

+0

我按照你的例子做了2個改變:a)TestLocal有一個方法由TestBean實現(沒有這個,weblogic不能部署)b)創建一個調用EJB的servlet。錯誤仍然發生。 – Topera 2010-08-18 01:06:28

+0

@Topera我真的不確定(我甚至不確定我的建議對於EJB 3.0是必須的)。我可能是錯的,但這看起來像一個錯誤。我建議聯繫支持 – 2010-08-19 21:21:12

+0

呃....這是我唯一的答案.... + 1並且接受! :) – Topera 2010-08-20 12:57:09

7

您無法使用@PostConstruct在無狀態bean EJB 3中創建計時器。有關說明,請參閱此博客How to use EJB 3 timer in a weblogic 10 cluster environment。甚至博客也在談論weblogic,但解釋也應該適用於其他應用程序服務器。

+0

是的,同樣的問題發生在JBoss – icordoba 2014-07-21 10:52:09

+0

雖然這解釋了爲什麼它不可能,但它並沒有給出問題的實際答案。 – maja 2015-07-14 13:57:32

1

容器將不允許在使用無狀態會話Bean的@PostConstruct註釋的方法中使用timerService。如果你想在用@PostConstruct註釋的方法中使用timerService,那麼Singleton會話bean(@Singleton)就是這樣。

相關問題