File Montoring | Not updating in jsp while listening

292 views Asked by At

I am monitoring the file creation and modification in directory using commons-io jar.I am able to get the results in Eclipse console.

final long pollingInterval = 5 * 1000;
     String FOLDER = "C:/test";
    File folder = new File(FOLDER);
    folder.setReadable(true);
    if (!folder.exists()) {
        // Test to see if monitored folder exists
        throw new RuntimeException("Directory not found: " + FOLDER);
    }

    FileAlterationObserver observer = new FileAlterationObserver(folder);
    FileAlterationMonitor monitor =
            new FileAlterationMonitor(pollingInterval);


    FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        // Is triggered when a file is created in the monitored folder

        @Override
        public void onFileCreate(File file) {
            try {
                // "file" is the reference to the newly created file
                System.out.println("File created: "
                        + file.getCanonicalPath());
                getNewMethod(file);// here in this method i am not able to return since its void.

            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }
        @Override
        public void onFileChange(File file) {
            try {

                System.out.println("File modified: "
                        + file.getCanonicalPath());
                getNewMethod(file); // here in this method i am not able to return since its void.
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }

    };
    observer.addListener(listener);
    monitor.addObserver(observer);
    monitor.start();

The problem is that i cannot able to return the file name when it is calling onFileCreate and onFileChange methods.How to achieve this? And also i am trying to call one method inside onFileCreate and onFileChange which returns a list.How to return the list? Because in this listener i dont see return parameter except void.

//Calling a newMethod

public String getNewMethod(File newfile) throws IOException{

    System.out.println("getList method called : "+newfile.getCanonicalPath());
     return "redirect:finalPage"; // here the redirection is not happening

}

When i see file change/create event is triggered, i need the changes to be update in jsp.How to achieve this?

2

There are 2 answers

1
janasainik On

You can create File object before you create listener and set file in onFileCreate() method. You can also call the methods within anonymous class methods by using OuterClass.this.OuterClassMethod().

File file;
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        // Is triggered when a file is created in the monitored folder

        @Override
        public void onFileCreate(File file) {
            try {
                // "file" is the reference to the newly created file
this.file = file;                
System.out.println("File created: "
                        + file.getCanonicalPath());
OuterClassName.this.newMethod();

            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }
        @Override
        public void onFileChange(File file) {
            try {

                System.out.println("File modified: "
                        + file.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }

    };
public list newMethod(){
// returns list here
List list = new ArrayList();
return list;
}

Note: Replace OuterClassName with actual outer class name.

1
Vishal Zanzrukia On
import java.io.File;


public class Test {

    public void testMonitoring {

        final long pollingInterval = 5 * 1000;
        String FOLDER = "C:/test";
       File folder = new File(FOLDER);
       folder.setReadable(true);

       if (!folder.exists()) {
           // Test to see if monitored folder exists
           throw new RuntimeException("Directory not found: " + FOLDER);
       }

       FileAlterationObserver observer = new FileAlterationObserver(folder);
       FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
       FileAlterationListener listener = new FileAlterationListenerAdaptor() 
       {
           // Is triggered when a file is created in the monitored folder

           private String filePath;

           @Override
           public void onFileCreate(File file) {
               try {
                   // "file" is the reference to the newly created file
                   System.out.println("File created: "
                           + file.getCanonicalPath());

                   setFilePath(file.getCanonicalPath());


               } catch (IOException e) {
                   e.printStackTrace(System.err);
               }

           }
           @Override
           public void onFileChange(File file) {
               try {

                   System.out.println("File modified: "
                           + file.getCanonicalPath());
               } catch (IOException e) {
                   e.printStackTrace(System.err);
               }

           }
        /**
         * @return the filePath
         */
        public String getFilePath() {
            return filePath;
        }
        /**
         * @param filePath the filePath to set
         */
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

       };
       observer.addListener(listener);

       //getting the file path
       System.out.println(listener.getFilePath());

       monitor.addObserver(observer);
       monitor.start();
    }

}