Reputation: 3026
We are starting an external process from a Servlet.
try {
Process proc = Runtime.getRuntime().exec("java -jar " + jarLocation );
The servlet and the external process access the same database with the same userid/pwd.
The servlet does not wait for the external process to finish.
The servlet finishes its' processing in less than a second.
When started from the Servlet, the external app takes at least two minutes to complete.
When run in stand-alone mode the external app takes about 5 seconds to complete.
We tried stopping the web app after the external process was started and this resulted in the external process finishing immediately.
So, there appears to be some sort of interraction between the Servlet and the external process that is causing the process to take way too long to complete. We have been trying to figure out what could be happening for a couple of days and have gotten nowhere.
Does anybody have any idea what could be going on here?
Upvotes: 0
Views: 611
Reputation: 272307
You need to consume the spawned process' stdout/stderr in the servlet process.
Otherwise the spawned process will likely block waiting for it to be consumed. There's a little complexity in doing this - see this answer and its linked article for more info.
Note that you should perform a Process.waitFor()
to collect the spawned process exit code. Otherwise you'll have a zombie on your hands. Consequently you may wish to wrap all this in a separate thread such that your servlet can spawn the process and return immediately.
Having said all that, if you're launching a new Java process, can't you simply refactor it such that you call it as a library function from within the same JVM ? Spawning processes is a relatively heavy-weight and time consuming task.
Upvotes: 2