是否可以通过 JMX 以编程方式关闭 Java 进程
我目前正在编写一个应用程序来监视另一个 Java 进程,并在达到某些目标时采取特定操作。例如,如果线程死锁一段时间,则终止该线程,如果内存使用量超过特定数量,则发送电子邮件警报并终止进程等。
我的应用程序将作为独立应用程序运行,监视特定的其他应用程序应用程序(本地,但据我所知,远程或本地没有区别)。
我正在通过 MXBeans 监视外部 JVM,但没有找到一种干净的方法来终止外部进程,除非使用像“kill -9”这样的系统调用(顺便说一句,我正在 UNIX 中工作)。
有没有办法通过 MXBean 接口杀死 JVM?
格雷厄姆
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然。在目标服务器上实现一个调用 System.exit() 的 MBean,并从客户端将其作为 JMX 操作调用。
Sure. Implement an MBean on the target server that calls
System.exit()
, and invoke that as a JMX operation from the client.如果您使用 Spring,您可以简单地注释您的 bean,使其操作之一公开为 MBean 操作.所以它会是这样的:
...或者也许自己停止应用程序上下文。
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:
... or perhaps stop the application context yourself.