How often is a programmatic created EJB Timer executed in a cluster?

安稳与你 提交于 2019-12-05 13:16:59

First, make sure you have setup timer-service to external shared XA datasource as described here.

Having digged into your question in past, I remember some explanation by devs in mailing-lists, that Glassfish's implementation is as follows:

Say you have node A, B and C in a cluster. Persistent timers created at node A are "owned" by node A (that is timer-events are delivered to node A). If node A fails, then its timers can be migrated to another live node.

Having that Glassfish doesn't support cluster-wide @Singletons, you end up with as many timers as calls to initResetTimer(). Moreover, each server restart/redeploy will possibly create a new instance of timer per cluster node, in additions to old uncancelled ones, so don't forget to cancel your programmatically created timers :) To avoid this alltogether use declarative @Schedule(...) approach and Glassfish will create the timer once across cluster and, hopefully, automatically migrate them on failure.

Hope this helps.

UPDATE:

A programmatically created timer, persistent or non-persistent, will be fired in the JVM/node it was created, regardless of clustered setup or not. You can sum up roughly: number of independent timer instances is equal to the number of calls to timer.createXxxTimer()

ewernli

I had a look at chapter 18 "Timer Service" of the EJB 3.1 spec. The app should behave according to the spec independently of clustering.

My understanding is that if createIntervalTimer is called once in a cluster, the timer should fire once independently on the number of nodes in the cluster. Since each singleton bean (according to your question) calls createIntervalTimer, the it will be execute n times. It is similar to creating timers in ServletContextListener.

This is theory, though. I would double-check for the specific app server you target. In glassfish, cluster-wide timer require to configure the timer pool with an external database.

Even if it is not a direct amswer this could help anyway : One way that would configure only one instance per clustered environment would be to expose the singleton ejb as MXbean. You should have to expose a managed imterface, that could even be empty , then register your ejb to the jmx service in the @PostCostruct marked method. At last you would have to provide a @PreDestroy hook to deregister from the jmx service. This is the way suggested by the Java Champion Adam Bien.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!