4

I'm using commons VFS to monitor certain folder for changes (mainly inserting new file), the program should runs permanently, I use the following code

FileSystemManager fsManager = VFS.getManager();
FileObject listendir = fsManager.resolveFile(path);
DefaultFileMonitor fm = new DefaultFileMonitor(new VfsListener());
fm.setRecursive(true);
fm.addFile(listendir);
fm.start();

where path is the folder path, and VfsListener is a class that implements FileListener, when I run the program it runs and then closed immediately, when I added this after fm.start() :

Thread.sleep(100000)

the program run for a while and then closed after the time out is reached, and I don't want that, I want the program to rum permanently, if anyone know please reply

Michael Samir
  • 165
  • 1
  • 4
  • 14

1 Answers1

5

VFS starts the FileMonitor thread as a daemon thread in low priority. The definition of the method setDaemon(boolean) states that

Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

This method must be called before the thread is started.

This is the reason your program works as long as you 'sleep' in the main thread. However, this is only an issue if you're running this program as a standalone java program. If you run the same piece of code in a application server like Jboss, the code just works fine.

If you still want the standalone program to wait indefinitely, you can modify the program to use a ThreadPoolExecutor which will essentially wait for the new tasks to be available in the Task Queue.

public static void main(String[] args) throws FileSystemException {
    Executor runner = Executors.newFixedThreadPool(1);
    runner.execute(new Runnable() {

        @Override
        public void run() {
            FileObject listendir = null;
            try {
                FileSystemManager fsManager = VFS.getManager();
                listendir = fsManager.resolveFile(absolutePath);
            } catch (FileSystemException e) {
                e.printStackTrace();
            }
            DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener() {

                @Override
                public void fileDeleted(FileChangeEvent event) throws Exception {
                    System.out.println(event.getFile().getName().getPath()+" Deleted.");
                }

                @Override
                public void fileCreated(FileChangeEvent event) throws Exception {
                    System.out.println(event.getFile().getName().getPath()+" Created.");
                }

                @Override
                public void fileChanged(FileChangeEvent event) throws Exception {
                    System.out.println(event.getFile().getName().getPath()+" Changed.");
                }
            });
            fm.setRecursive(true);
            fm.addFile(listendir);
            fm.start();
        }
    });
}

Hope this helps.

saratbeesa
  • 411
  • 3
  • 6