Run @Scheduled task only on one WebLogic cluster n

2019-03-25 03:53发布

We are running a Spring 3.0.x web application (.war) with a nightly @Scheduled job in a clustered WebLogic 10.3.4 environment. However, as the application is deployed to each node (using the deployment wizard in the AdminServer's web console), the job is started on each node every night thus running multiple times concurrently.

How can we prevent this from happening?

I know that libraries like Quartz allow coordinating jobs inside clustered environment by means of a database lock table or I could even implement something like this myself. But since this seems to be a fairly common scenario I wonder if Spring does not already come with an option how to easily circumvent this problem without having to add new libraries to my project or putting in manual workarounds.

  • We are not able to upgrade to Spring 3.1 with configuration profiles, as mentioned here

Please let me know if there are any open questions. I also asked this question on the Spring Community forums. Thanks a lot for your help.

8条回答
Deceive 欺骗
2楼-- · 2019-03-25 04:34

You don't neeed to synchronize your job start using a DB. On a weblogic application you can get the instanze name where the application is running:

String serverName = System.getProperty("weblogic.Name");

Simply put a condition two execute the job:

if (serverName.equals(".....")) {
  execute my job;
}

If you want to bounce your job from one machine to the other, you can get the current day in the year, and if it is odd you execute on a machine, if it is even you execute the job on the other one. This way you load a different machine every day.

查看更多
Root(大扎)
3楼-- · 2019-03-25 04:35

Be careful, since in the solution of implementing your own synchronization logic using a shared lock table, you always have the concurrency issue where the two cluster nodes are reading/writing from the table at the same time.

Best is to perform the following steps in one db transaction: - read the value in the shared lock table - if no other node is having the lock, take the lock - update the table indicating you take the lock

查看更多
登录 后发表回答