Not able to stop the listener using separate method

I have create a File watcher using The file changes are captured correctly. But I want to stop the monitor task using a separate method. Its not working. The source code is as below.

import; import; import java.util.Scanner; import; import; import; import; import; import; public class FileMonitor2 { //public final class FileMonitorExample { private static final String EXAMPLE_PATH = "D:\\testrail\\install.txt"; private static final String PARENT_DIR1 = "D:\\ibi\\DevStudio77\\client\\wfc\\etc"; private static final String PARENT_DIR2 = "D:\\ibi\\DevStudio77\\config"; public static void runExample(boolean b) { System.out.println("File Monitor example..."); FileAlterationMonitor monitor=new FileAlterationMonitor(); // FileEntry // We can monitor changes and get information about files // using the methods of this class. if(b){ FileEntry entry = new FileEntry(FileUtils.getFile(EXAMPLE_PATH)); System.out.println("File monitored: " + entry.getFile()); System.out.println("File name: " + entry.getName()); System.out.println("Is the file a directory?: " + entry.isDirectory()); // File Monitoring // Create a new observer for the folder and add a listener // that will handle the events in a specific directory and take action. File parentDir = FileUtils.getFile(PARENT_DIR1); FileAlterationObserver observer = new FileAlterationObserver(parentDir); observer.addListener(new FLA2()); File parentDir1 = FileUtils.getFile(PARENT_DIR2); FileAlterationObserver observer2 = new FileAlterationObserver(parentDir1); // observer.addListener(new FLA()); observer2.addListener(new FLA()); // Add a monior that will check for events every x ms, // and attach all the different observers that we want. monitor.addObserver(observer); //monitor = new FileAlterationMonitor(500, observer); monitor.addObserver(observer2); try { monitor.start(); System.out.println("Started"); // After we attached the monitor, we can create some files and directories // and see what happens! //monitor.stop(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }else{ /*System.out.println("type the value"); Scanner sc=new Scanner(; String; if(ss.equals("aa")){*/ try { monitor.stop(); System.out.println("stopped"); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } // } } } public static void main(String args[]) throws InterruptedException{ runExample(true); System.out.println("After start"); Thread.sleep(1000); runExample(false); } //} }

-------------Problems Reply------------

When runExample(false) is called the second time, the monitor is recreated. You need to called monitor.stop() on the instance which was created when you called runExample(true). You can save the monitor to a static variable after it is created the first time. Any subsequent calls to runExample(false) then has access to the original instance of monitor.

You can fix this by declaring monitor outside of the runExample method, and only create a new monitor if true was passed:

static FileAlterationMonitor monitor;

public static void runExample(boolean b) {
if (b) {
monitor = new FileAlterationMonitor();
} else {
if (monitor == null) return;

Category:java Views:6 Time:2018-05-14

Related post

Copyright (C), All Rights Reserved.

processed in 0.149 (s). 11 q(s)