Sometimes, you need to stop/re-route a batch Java processing. For
example, a program is processing a large result set from a database, it
takes too long and you need to stop it gracefully without killing the
process. If this program is deployed in production, you may run another
program or script that will make an OS level change that your long
running program will recognize. For example, you can start another
program that will create a small file with any content in a particular
directory. The very fact of existence of this file has to stop the
running program. Try to run the program below. It’ll stop as soon as
you will create the file c:\abc.txt
import java.io.File;
public class FIleCheck {
public static void main(String[] args) {
File killSignal = new File( “c:\\abc.txt “);
while (!killSignal.exists()){
System.out.println( “Processing the next row from a database… “);
}
}
}
If you do not want to do a file check on each loop iteration, use Java modulo operator to do it on every 100th read:
if ((i %100) == 0 amp; amp; killSignal.exists()) break;
There are other ways of interaction between a running Java program and the outside world, but this seems to be the simplest one.