使@Schedule仅在群集环境中运行一次 [英] Making @Schedule run only once in a clustered environment

查看:761
本文介绍了使@Schedule仅在群集环境中运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个tomee实例聚集。

I have two tomee instances clustered.

每个人都有一个注释方法,如

Each one have a method annotated like

@Schedule(dayOfWeek = "*")
public void runMeDaily() {...}

我想每天只运行一次这种方法。不是一天两次(每个实例一次)

I'd like to run this method only once a day. Not twice a day (one on each instance)

我可以使用这里描述的标志仅在一个WebLogic群集节点上运行@Scheduled任务?或者只选择一个节点,但我想知道是否有更优雅的方式这样做。

I could use a flag as described here Run @Scheduled task only on one WebLogic cluster node? or just elect some node, but I wonder if there's a more elegant way to do that.

这个问题与集群环境中的EJB3.1 @Schedule 但我没有使用JBOSS。 (并且没有回答)

This question is somewhat related to EJB3.1 @Schedule in clustered environment but I am not using JBOSS. (and it's not answered)

推荐答案

我只能使用特定于平台的非Java EE解决方案来解决这个问题)。就我而言,我使用的是TomEE +和Quartz。在集群模式下运行Quartz(org.quartz.jobStore.isClustered = true)并将计时器保存在单个数据库中会强制Quartz选择一个实例来触发计时器,因此它只运行一次。

I could only solve this using a non-Java EE solution, specific to the platform (proprietary). In my case, I am using TomEE+ and Quartz. Running Quartz in the clustered mode (org.quartz.jobStore.isClustered = true) and persisting the timers in a single database forces Quartz to choose an instance to trigger the timer, so it will only run once.

此链接非常有用 - http://rmannibucau.wordpress.com/2012/08/22/tomee-quartz-configuration-for-scheduled-methods/

This link was very useful -- http://rmannibucau.wordpress.com/2012/08/22/tomee-quartz-configuration-for-scheduled-methods/

遗憾的是,Java EE没有为此指定行为。 (但我希望): - )

It's a shame Java EE does not specify a behavior for that. (yet, I hope) :-)

这篇关于使@Schedule仅在群集环境中运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆