-1

I have an spring batch application which is deployed on multiple servers (multiple JVMs). All the instances listen the common shared path for input file.

How can I ensure that, input file kept at shared location will be consumed by only one instance?

Sachin Mhetre
  • 4,465
  • 10
  • 43
  • 68
  • Use a unique instance to read the folder and share to the instances the files to treat (populate a thread safe queue or any other solution).. That instance won't do anything else. – AxelH Mar 21 '18 at 12:20
  • @AxelH I am not sure whether this will work for multiple JVMs. Where we are gonna maintain the queue? On shared system? – Sachin Mhetre Mar 21 '18 at 13:19
  • Different servers... my bad ! I am not an expert at this level of concurrency so I don't see how to manage it properly. There is hacks but I would not suggest those too quickly. – AxelH Mar 21 '18 at 13:24
  • Excellent!!! Downvote without any comments. – Sachin Mhetre Mar 23 '18 at 08:24

1 Answers1

-2

This can be maintained using enum status in db RUNNING, NOT_RUNNING. when thread 1 going to start to work on the file it will fetch scheduler status entity and check the scheduler status. If it is not running then update the status as RUNNING and execute the folder and after completing mark it as NOT_RUNNING. This code should be get executed on single environment and synchronized manner. It can be invoked using RMI call from all batches to acquire lock on the location.

Dhruv Raj Singh
  • 302
  • 2
  • 12
  • What will happen if two instances (threads) start at the same time? They read the status at the same time i.e. NOT_RUNNING. Both the threads will start processing the file. – Sachin Mhetre Mar 21 '18 at 13:16
  • I think you didnt read the answer properly. please re look, This code should be get executed on single environment and synchronized manner. It can be invoked using RMI call from all batches to acquire lock on the location. It means the code which is acquring lock by fetching status is accessed by single thread only. – Dhruv Raj Singh Mar 23 '18 at 10:17
  • So, when you can't answer the question, you downvote it. @SachinMhetre didnt get you – Dhruv Raj Singh Mar 23 '18 at 10:19