user903724
user903724

Reputation: 3026

Servlet starting external process

We are starting an external process from a Servlet.

try {
    Process proc = Runtime.getRuntime().exec("java  -jar " + jarLocation );

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

Answers (1)

Brian Agnew
Brian Agnew

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

Related Questions