Question : I want to execute java jar file from webservice in spring boot project. and want to monitor it.
Problem : I am able to execute this process but problem is that process did not processed further.
I want to know why this process is waiting and why its not processed. how can i monitor its progress.
its get processed on following conditions:
- Once i stop the spring boot project or tomcat.
- Its get processed if i remove
process.waitFor();
I tried the solution from this, that is execute the process from another thread.
My web service call
@RequestMapping(value="/startAnalysis", method=RequestMethod.POST) public String startAnalysis() { List<String> cmd = new ArrayList<>(); cmd.add("java"); cmd.add("-jar"); cmd.add("test.jar"); try { //Process p = Runtime.getRuntime().exec(cmd.toArray(new String[0])); //ProcMon procMon = new ProcMon(cmd); //Thread t = new Thread(procMon); //t.setName("procMon"); //t.start(); ProcessBuilder processBuilder = new ProcessBuilder(cmd); Process process = processBuilder.start(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } return "success"; }
Thanks in advanced.
2 Answers
Answers 1
Often the process waits for it's output to be consumed by the calling process. Sometimes this simply means the output gets displayed on the terminal but in this case you might need to read the inputstream until it blocks. If you don't know how the process is supposed to behave you might just want to read the process.getInputStream() in a separate thread. It's also possible your process is waiting for something to be written to the process.getOutputStream() in some states.
Either you need to check the documentation of the jar, or try it by executing it straight from a command prompt/shell and see how it behaves. Then you can change your application to read the output as you expect it to behave.
In lots of applications the output is most easily read line-by-line:
final String EXPECTED_OUTPUT = "Hello World"; BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String firstLine = reader.readLine(); if (!firstLine.equals(EXPECTED_OUTPUT)) { // handle unexpected situation } // maybe handle some more output // or send something to the process.getOutputStream() in response // and finally wait for the application to exit when it should be done process.waitFor();
Answers 2
Please find an elaborate reasoning of why a call to process.waitFor()
would not return at all in some cases, here.
Alternatively, see if its possible for you to use the alternate version of waitFor()
method where we provide the timeOut value.
0 comments:
Post a Comment