1

My situation:

There is a analysis operation in our Java EE application, this function is implemented by executing a Batch Command using java.lang.Runtime. The most serious matter is that this operation would take a long time, and during execution, some errors would be encountered, and then the whole process tree is still alive, which undoubtedly lower the performance of application and server. So I was wondering that anybody can please offer a idea to safely and correctly solve this issue.

And I know that this is a very common question in our development, I will be greatly appreciated if there is a example provided here.

Brady Chu
  • 21
  • 4

2 Answers2

1

If you started the Application with

Process p = Runtime.exec("");

then you can call the

p.destroy();

Other than that I think it would be great if you can make the batch process destroy itself if any error occurred in execution. Refer this question for more details.

Community
  • 1
  • 1
Chan
  • 2,601
  • 6
  • 28
  • 45
0

See http://commons.apache.org/exec/apidocs/index.html

Interface CommandLauncher contains several exec methods that return Process.

But anyway you do not have any way to control the process ID: it is the OS responsibility. Moreover standard java API does not allow you even to retrieve the process ID. There was a trick in older java versions: the implementation of Process contained int field pid that could be retrieved using reflection. But this was changed in version 1.6.

Jason
  • 1,241
  • 8
  • 16