如何使用@postConstructs在无状态BEAN EJB3中创建计时器?
-
28-09-2019 - |
题
当在池中创建无状态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 ins ins ins not 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();
}
servlet
public class TestServlet extends HttpServlet {
@EJB
private TestLocal test;
protected void doGet(....) throws .... {
test.test();
}
}
细节
我正在使用WebLogic Server 11G。
解决方案
我不是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:无论如何,您当前遇到的错误报告的报告很少,您可能应该打开一个案例。
其他提示
您无法使用@postConstruct在无状态bean EJB中创建计时器3。请参阅此博客 如何在WebLogic 10群集环境中使用EJB 3计时器 用于解释。甚至博客都在谈论博客,但是解释也应适用于其他应用程序服务器。
容器将不允许使用无状态会话bean的@postConstruct注释的方法中的计时服务。如果您想在用@postConstruct the for Singleton Session Bean(@singleton)注释的方法中使用TimerService。