简体   繁体   English

Java:防止在多个实例上创建相同的任务

[英]Java: Prevent creating same task on multiple instances

I'm in the middle of creating some Data Processor. 我正在创建一些数据处理器。 It has next API method: create some task, get task result. 它具有下一个API方法:创建一些任务,获取任务结果。

Data Processors should recover if fail and allowed to be run in multiple instances. 如果出现故障并允许在多个实例中运行,则数据处理器应恢复。 (DP1, DP2, etc). (DP1,DP2等)。

I store the state in Redis db. 我将状态存储在Redis数据库中。 For example: 例如:

API creates task (Runnable), put it into the scheduledThreadPoolExecutor, and run it every 5 seconds, modifying Redis state. API创建任务(Runnable),将其放入ScheduledThreadPoolExecutor,然后每5秒运行一次,以修改Redis状态。 After getting the results of tasks, task is cancelled and Redis state is erased. 获取任务结果后,将取消任务并清除Redis状态。

If DataProcessor fails and restart, I get the state from the Redis Database, re-create tasks and everything is OK. 如果DataProcessor失败并重新启动,我将从Redis数据库获取状态,重新创建任务,一切正常。

But if another DataProcessor instance is run, it also takes state from Redis, re-create tasks and runs them, which is not preferred, as these tasks are already processed by another instance of DataProcessor. 但是,如果运行另一个DataProcessor实例,它也会从Redis获取状态,重新创建任务并运行它们,这不是首选方法,因为这些任务已经由DataProcessor的另一个实例处理。

I can't even mark this Redis state as [not processed anymore] in case of fail, because the instance which knows that it failed is.. well, failed and can't do anything anymore. 如果发生故障,我什至无法将Redis状态标记为[不再处理],因为知道失败的实例是..很好,失败了,无法执行任何操作。

What can be done in this situation? 在这种情况下可以做什么?

找到“ Redisson”库,它具有分布式调度程序,正是我需要的。

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

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