How to make org.apache.commons.io.monitor behave multi-threaded?

535 Views Asked by At

In my Java micro-service, I am overriding onFileCreate() function. This method exist in inbuilt library = org.apache.commons.io.monitor, class = FileAlterationListenerAdaptor, method = void onFileCreate(final File file) . I noticed that even if there are multiple files created, there is only a single thread which is listening to file creations. That means it processes files one by one (synchronous) , instead of multiple at the same time. How can I achieve multi-threading behavior here?

I don't know if it is relevant but I noticed that some of the methods defined in this inbuilt library are 'synchronized'. I am talking about class=FileAlterationMonitor, methods= setThreadFactory(), start(), stop(). Is that the reason? If yes, do I need to override all these 3 methods, or some of them? enter image description here

1

There are 1 best solutions below

2
Alexander Pavlov On

setThreadFactory will not help you, it is just an alternative way to create that single thread which monitors file system.

What you need to do is

  1. Create thread pool which will do work for new files. This way you can control how many new files you process in parallel (trust me, you do not want unlimited concurrency)
  2. Your FileAlterationListenerAdaptor.onFileCreate should not process file by itself. Instead, it should submit task to thread pool

Roughly, code should be something like that

int numberOfThreads = ...;
ExecutorService pool = java.util.concurrent.Executors.newFixedThreadPool(numberOfThreads);

FileAlterationListenerAdaptor adaptor = new FileAlterationListenerAdaptor() {
  public void onFileCreate(final File file) {
    pool.submit(new Runnable() {
       // here you do file processing
       doSomethingWithFile(file);
    });

  }
}

....

FileAlterationObserver observer = new FileAlterationObserver(directory);
observer.addListener(adaptor);
...

FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
monitor.addObserver(observer);
monitor.start();