简体   繁体   中英

Is it possible to programmatically close a Java process through JMX

I'm currently writing an app to monitor another Java process and take specific actions when certain targets are hit. For example, if a thread deadlocks for a certain time, kill the thread, if the memory usage goes over a specific amount, send email alerts and kill the process, etc.

My app will run as a stand-alone app, monitoring specific other apps (locally, though from what I can see remote or local makes no difference here).

I'm monitoring the external JVMs via MXBeans, but cannot see a clean way to kill the external process short of a system call like 'kill -9 ' (I'm working in UNIX by the way).

Is there any way to kill a JVM through the MXBean interfaces?

Graham

Sure. Implement an MBean on the target server that calls System.exit() , and invoke that as a JMX operation from the client.

If you're using Spring, you can simply annotate your bean to have one of its operations being exposed as an MBean operation . So it would be something like this:

@MBeanOperation(description="Kill the service")
public void die() {
  System.exit();
}

... or perhaps stop the application context yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM