سؤال

I'm having trouble keeping the vfs2's DefaultFileMonitor thread alive. The main thread of execution just terminates gracefully after the monitor object has been started. I'm wondering why this object wouldn't be "monitoring" and just goes right to the end instead. (ends with the log message "exitting....")

public static void main(String[] args) {
    try {
        Options options = new Options();

        options.addOption("b", true, "path to the build file");
        options.addOption("d", true, "directory to watch");
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        String dir = cmd.getOptionValue("d");
        String buildFile = cmd.getOptionValue("b");

        if(dir == null) {
            logger.error("No directory specified," +
                          " use [-d 'name_of_dir'] to specify one");
            return;
        }


        if(buildFile == null) {
            logger.error("No build file path specified," +
                          " use [-b 'path_to_build_file'] to specify one");
            return;
        }

        FileSystemManager fsManager = VFS.getManager();
        FileObject listendir = fsManager.resolveFile(dir);


        DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener(buildFile));
        fm.setRecursive(true);
        fm.addFile(listendir);
        fm.start();
    }catch(Exception e){
        logger.error("Exception ", e);
    }
    logger.info("exitting....");
}
هل كانت مفيدة؟

المحلول

DefaultFileMonitor works like a daemon thread, i.e., the virtual machine terminates even if monitor thread is running. A workaround is to use a non-daemon thread with a infinite loop or another kind of loop that it's under your control.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top