简体   繁体   English

是否可以通过JMX以编程方式关闭Java进程

[英]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. 我正在编写一个应用程序来监视另一个Java进程,并在遇到某些目标时采取特定的操作。 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). 我正在通过MXBeans监视外部JVM,但看不到一种干净的方法来杀死外部进程,而不是像'kill -9'这样的系统调用(顺便说一句,我在UNIX中工作)。

Is there any way to kill a JVM through the MXBean interfaces? 有没有办法通过MXBean接口杀死JVM?

Graham 格雷厄姆

Sure. 当然。 Implement an MBean on the target server that calls System.exit() , and invoke that as a JMX operation from the client. 在目标服务器上实现一个调用System.exit()的MBean,并从客户端调用它作为JMX操作。

If you're using Spring, you can simply annotate your bean to have one of its operations being exposed as an MBean operation . 如果您正在使用Spring,则可以简单地注释您的bean,使其中一个操作作为MBean操作公开。 So it would be something like this: 所以它会是这样的:

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

... or perhaps stop the application context yourself. ...或者可能自己停止应用程序上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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