简体   繁体   English

当应用程序有两个实例时,如何避免同时执行石英作业

[英]How to avoid simultaneous quartz job execution when application has two instances

I've got a Spring Web application that's running on two different instances. 我有一个在两个不同实例上运行的Spring Web应用程序。 The two instances aren't aware of each other, they run on distinct servers. 这两个实例彼此不知道,它们在不同的服务器上运行。

That application has a scheduled Quartz job but my problem is that the job shouldn't execute simultaneously on the instances, as its a mail sending job, it could cause duplicate emails being sent. 该应用程序具有计划的Quartz作业,但是我的问题是该作业不应在实例上同时执行,因为它的邮件发送作业可能会导致重复发送电子邮件。

I'm using RAMJobStore and JDBCJobStore is not an option for me due to the large number of tables it requires.(I cant afford to create many tables due to internal restriction) 我正在使用RAMJobStore,但由于需要大量表,因此对我来说不是JDBCJobStore(由于内部限制,我无法创建许多表)

The solutions I thought about: -creating a single control table, that has to be checked everytime a job starts (with repeatable read isolation level to avoid concurrency issues) The problem is that if the server is killed, the table might be left in a invalid state. 我考虑过的解决方案:-创建单个控制表,每次作业启动时都必须检查该表(具有可重复的读取隔离级别,以避免并发问题)问题是,如果服务器被杀死,则该表可能会留在服务器中。无效状态。

-using properties to define a single server to be the job running server. -使用属性将单个服务器定义为作业运行服务器。 Problem is that if that server goes down, jobs will stop running 问题是,如果该服务器关闭,作业将停止运行

Has anyone ever experienced this problem and do you have any thoughts to share? 有没有人遇到过这个问题,您有什么想法要分享吗?

Start with the second solution (deactivate qartz on all nodes except one). 从第二种解决方案开始(在一个节点上禁用所有节点上的qartz)。 It is very simple to do and it is safe. 这非常简单而且安全。 Count how frequently your server goes down. 计算服务器关闭的频率。 If it is inacceptable then try the first solution. 如果不可接受,请尝试第一种解决方案。 The problem with the first solution is that you need a good skill in mutithreaded programming to implement it without bugs. 第一个解决方案的问题是,您需要具备多线程编程方面的良好技能才能实现无错误的代码。 It is not so simple if multithreading is not your everyday task. 如果多线程不是您的日常任务,那不是那么简单。 And a cost of some bug in your implementation may be bigger than actual profit. 实现中的一些错误的成本可能大于实际利润。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM