2

I have implemented an application that process some xml input files and write more xml files as result.

I have to put this jar to run on a Raspberry Pi.

I transfer the input files from my pc to Raspberry Pi using PuTTY or WinSCP.

I have a method that read from a directory whose path I specified all the names of .xml files as filename.xml and add them into a list. Then I call other methods that process the inputFile on position 0 in list.

The inputFile is the file denoted by "path+filename".

After processing the inputFile from the position 0 in list, I delete the inputFile, and then the name from list.

My app should run permanently because the inputFiles may be sent at any time. Here is the first problem: I don't know how to make my jar permanently run.

Another problem is when there are no more files to read, because if the jar is still running, there an error will be thrown, because there are no more files.

I think I need my app to run only if there are xml file in the directory I set on Raspberry Pi, and I don't know hot to make it happen... There should be a trigger or something that make my app running only when xml files are sent to raspberry pi.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nelly Junior
  • 556
  • 2
  • 10
  • 30

1 Answers1

0

Let's say incoming file is in variable filename with path path as you told. We can wait until this file exists like this:

File f = new File(path+filename);
while(true){ //infinite loop for infinite executing
    while(!f.exists()){
        Thread.sleep(10000); //long time is ok (10 seconds)
    }
    readFile(f); //your code to read file
    //I recommend to delete or rename file to let f vaiable
}

To use it for folder you can use this:

File f = new File(path);
while(true){ //infinite loop for infinite executing
    while(f.listFiles().length==2){ //contains only . and .. "files"
        Thread.sleep(10000); //long time is ok (10 seconds)
    }
    readFolder(f.listFiles()); //your code to read folder (array of files)
}

And a bit better way to find out there is new file in folder added:

File f = new File(path);
int lastCount=2; //or =f.listFiles().length to skip change at start
while(true){ //infinite loop for infinite executing
    while(f.listFiles().length==lastCount){
        Thread.sleep(10000); //long time is ok (10 seconds)
    }
    readFolder(f.listFiles()); //your code to read folder (array of files)
    lastCount=f.listFiles().length; //setting we have just know how many files are there
}

Not tested, will work for every types of files, not only .xml. Filter should not cause much problems.

maskacovnik
  • 3,080
  • 5
  • 20
  • 26