5

プールにステートレスBeanが作成されたときに、タイマーEJB3を作成したいと思います。しかし、使用する@PostConstructと例外が発生します。

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を呼び出す場合、Beanはnullではありません。では、なぜこの例外が発生するのですか?


クラス

@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() {        
    }

}

インターフェース

@Local
public interface TesteLocal {

    void test();

}

サーブレット

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

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

詳細

weblogicサーバー11gを使用しています。

4

3 に答える 3

7

@PostConstructを使用して、ステートレスBean EJB 3にタイマーを作成することはできません。説明については、このブログ「weblogic10クラスタ環境でEJB3タイマーを使用する方法」を参照してください。ブログでさえweblogicについて話していましたが、その説明は他のアプリサーバーにも当てはまるはずです。

于 2010-11-17T14:44:25.337 に答える
2

コンテナは、ステートレスセッションBeanの@PostConstructで注釈が付けられたメソッドでtimerServiceを許可しません。@PostConstructで注釈が付けられたメソッドでtimerServiceを使用する場合は、go for SingletonセッションBean(@Singleton)を使用します。

于 2016-04-06T12:32:29.877 に答える
-3

100%確信はありませんが、 EJBタイマーを使用するには、Beanクラスにメソッドを実装するjavax.ejb.TimedObjectか、メソッドに注釈を付ける必要があると思います。@Timeout例:

@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:いずれにせよ、現在発生しているエラーはほとんど報告されていないので、おそらくケースを開く必要があります。

于 2010-08-15T23:27:58.770 に答える