我有一个用@Schedule注解的方法,容器偶尔会调用它。
@Schedule(second = "*/5", minute = "*", hour = "*", persistent = false)
public void myTimerMethod() throws Exception {
...
}问题是在某些情况下,我希望这个方法抛出一个异常,导致正在进行的事务回滚。但是,如果我这样做超过两次,计时器将被删除,并且不再调用!
INFO: EJB5119:Expunging timer ['68@@1359143163781@@server@@domain1' 'TimedObject = MyBean' 'Application = My-War' 'BEING_DELIVERED' 'PERIODIC' 'Container ID = 89072805830524936' 'Fri Jan 25 21:49:30 CET 2013' '0' '*/5 # * # * # * # * # * # * # null # null # null # true # myTimerMethod # 0' ] after [2] failed deliveries我知道我可以使用以下命令在domain.xml中配置计时器重新调度
<domains>
...
<configs>
<config>
...
<ejb-container session-store="${com.sun.aas.instanceRoot}/session-store">
<ejb-timer-service>
<property name="reschedule-failed-timer" value="true"></property>
</ejb-timer-service>
</ejb-container>
...
</config>
</configs>
...
</domains>但我的问题是,我可以在部署应用程序时配置此设置吗?
在以下位置找不到它:
glassfish-resources.xml
glassfish-ejb-jar.xml
glassfish-web.xml有没有什么办法可以通过编程来做到这一点?
(我之所以将这样的服务器配置放在配置文件中,而不是配置服务器,是为了让我的应用程序可以直接安装在新安装的glassfish上)
发布于 2013-01-28 16:46:30
我会使用不同的方法。
尝试引入一定程度的间接性,而不是直接从调度方法引发异常,如下所示:
...
@Inject RealWorkHere realImplementation;
@Schedule(second = "*/5", minute = "*", hour = "*", persistent = false)
public void myTimerMethod(){
try{
realImplementation.myTimerMethodImpl()
}catch (Exception x){
// hopefully log it somewhere
}
}
...其中RealWorkHere是具有实际实现的bean,如下所示:
@Stateless
public class RealWorkHere{
@TransactionAttribute(REQUIRES_NEW)
public void myTimerMethod() throws Exception {
}
}这带来了以下好处:
真正的业务事务不会在容器发起的事务中抛出异常(因此避免了真正的业务事务的expunging)
另请参阅
发布于 2013-08-19 05:05:23
在当前版本4之前的Glassfish中,如果在执行timeout回调方法期间发生的应用程序异常,则会删除计时器。
应用程序异常会导致回滚当前事务。在这种情况下,Glassfish会再次重试timeout回调方法的无错误执行。如果再次发生回滚,Glassfish将清除计时器。
我在Glassfish问题跟踪器中提交了一个问题,在发生异常的情况下不要删除计时器。Glassfish似乎是唯一一个在应用程序异常的情况下删除计时器的应用服务器。有关更多详细信息,请参阅glassfish #20749: Glassfish expunges timer even if callback method keeps its contract。希望你能投票支持我的问题。
我还提交了一个关于EJB规范的问题,以阐明EJB容器在这种情况下应该如何行为。有关更多详细信息,请参阅ejb-spec #111: Please clearify the behaviour of an container if an application exception is thrown during the execution of a timer callback method。
https://stackoverflow.com/questions/14530717
复制相似问题