0

I have defined a class(callable) named "folderwatcher" that is monitoring a specific folder for any changes. The main code is in an infinite loop similar to: Monitor a Directory for Changes using Java. I want the method/class that calls this "folderwatcher" to listen for any events discovered by the "folderwatcher" and does some operations based on the event. Does anyone know how I can do this? Do I need an event handler? any examples? thanks

Hossein
  • 40,161
  • 57
  • 141
  • 175

2 Answers2

3

First of all, it is wrong to use an infinite loop approach. if you are really using Callable then you are already using an ExecutorService. What you should use is a ScheduledExecutorService and schedule your code as a repeated task.

As far as an event handler, this is not hard to accomplish, for example:

interface FileDetectedHandler { void fileDetected(File f); }

public static void main(String[] args) {
  final ScheduledExecutorService scheduler = 
     Executors.newSingleThreadScheduledExecutor();
  final FileDetectedHandler h = new FileDetectedHandler(File f) {
    public void fileDetected(File f) {
      System.out.format("File detected: %s\n", f);
  }};
  final Runnable r = new Runnable() { public void run() {
    File f = null;
    //detect file
    if (f != null) h.fileDetected(f);
  }};
  scheduler.scheduleWithFixedDelay(r, 0, 2, TimeUnit.SECONDS);
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • thx for the example, could be please give me more details of the code. i am not really a pro in java. thx – Hossein Aug 22 '12 at 09:39
  • There is really not much more to it. I've updated with the two ExecutorService API calls, this is now a full example except for the file detection code that you already have. – Marko Topolnik Aug 22 '12 at 09:43
  • @MarkoTopolnik You probably meant: `scheduler.scheduleAtFixedRate(...)`. – assylias Aug 22 '12 at 09:54
  • @assylias Thanks, corrected (WithFixedDelay makes slightly more sense so I'll use that). – Marko Topolnik Aug 22 '12 at 10:05
  • @MarkoTopolnik the problem with ur approach is that, when I add/delete several files together to/from the "monitored" folder, it can only detect one operation for just one file. can you please help me that also? – Hossein Aug 22 '12 at 14:07
  • @MarkoTopolnik: i mean, as you said I removed the infinite loop and followed ur approach. now this happens. – Hossein Aug 22 '12 at 14:09
  • Well, post another question with your code included and I or someone else will be probably able to help you further. – Marko Topolnik Aug 22 '12 at 14:16
  • @MarkoTopolnik: http://stackoverflow.com/questions/12077391/how-to-monitor-a-folder-for-changes-in-java – Hossein Aug 22 '12 at 16:22
0

Use a callback. This can be done by implementing a synchronised method on the parent object and then pass a reference to the parent object into the runnable object's constructor. The runnable can then call the method on the parent to notify it of something.

http://en.wikipedia.org/wiki/Callback_(computer_programming)

Dan
  • 1,030
  • 5
  • 12