I am trying to implement a WatchService that watches a filesystem for changes. Once files get created I want to create tasks for each new file and add that task to a distributed queue to let it get processed from multiple machines.
My question now is how I get the different watchservices to not add the same task to the queue. Lets say machine 1 and machine 2 get the ENTRY_CREATE event at the same time and try to add it to the queue. Should I check before each
queue.offer(task)
wether that task is already present?
queue.contains(task)
Or is there a better way on the WatchService side?
Could it be bad if I have to check containsKeys(task) all the time or is this no problem? Or do you even have better ideas/approaches?
EDIT: The queue implementation I was going to use (https://docs.hazelcast.com/hazelcast/5.0/data-structures/priority-queue) provides the necessary synchronization and after reading the WatchService documentation again I also noticed, that the events do not always get processed in the same order. So my problems resolved themselves after reading those documentations again.