-1

I am trying to run a unix executable using java.

I have a UI which can be accessed by thousands of users. The executable can be triggered by a button.

I had a look at a few examples: Java Programming: call an exe from Java and passing parameters

What are the bottlenecks to using the suggested methods?

For example, if a user clicks on the button and runs the executable, would this affect the performance for the second user?

Or if they press the button at the same time, what is going to happen?

Or how would I know if the execution is completed(I don't think OS is going to manage for me).

Community
  • 1
  • 1
Gokhan Dilek
  • 4,314
  • 4
  • 21
  • 24

1 Answers1

1

What are the bottlenecks to using the suggested methods?

The biggest problem with this is your hardware and the executable itself. If the executable completes by itself after some time, you should be fine. You don't want 1000's of processes waiting for something to complete. You need to also monitor the processes once they started and kill them if they reach a timeout. Another consideration is limiting the number of concurrent processes per user.

For example, if a user clicks on the button and runs the executable, would this affect the performance for the second user?

Not directly. See above.

Or if they press the button at the same time, what is going to happen?

If you start your process on a new thread (or task scheduler) each time, nothing. each time the button is pressed a new process is started.

Or how would I know if the execution is completed(I don't think OS is going to manage for me).

Here is an example:

String command = "PATH_TO_EXECUTABLE/EXECUTABLE PARAMS ";
log.info("Executing command - " + command);
// Executing the command
Process process;
ArrayList<String> outLines = new ArrayList<String>();
try {
  process = Runtime.getRuntime().exec(command);

  // Getting the results
  process.getOutputStream().close();
  String line;
  log.info("Standard Output:");
  BufferedReader stdout = new BufferedReader(new InputStreamReader(
      process.getInputStream()));

  while ((line = stdout.readLine()) != null) {
    log.info(line);
    outLines.add(line);
  }

  stdout.close();
  log.info("Standard Error:");
  BufferedReader stderr = new BufferedReader(new InputStreamReader(
      process.getErrorStream()));
  while ((line = stderr.readLine()) != null) {
    log.info(line);
  }
  stderr.close();
  log.info("Done");

} catch (IOException e) {
  log.error("Error while exceuting command", e);
}
String result = outLines.get(outLines.size()-1);
//TODO do something with the results
//at this point the process has finished executing
Viorel Florian
  • 594
  • 9
  • 29
  • "You need to also monitor the processes once they started and kill them if they reach a timeout." How do I exactly do this? Should I write a different application which monitors it? Or should I try to get a third party app to monitor a specific process and kill it after sometime? – Gokhan Dilek Nov 24 '16 at 01:22
  • 1
    You can view this example and merge some of the functionality into mine. http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when-using-javas-runtime-exec – Viorel Florian Nov 24 '16 at 09:28