java 向正在运行的 JVM 发送信号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5023520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Signals to a Running JVM
提问by jmq
I'm using a custom signal handler to catch TERM, ABRT and INT signals in a custom java daemon. I have this handler in the code so that I can send TERM signals to it and gracefully shutdown the program via the kill command. The signal handler works right now, but when I compile the code I'm receiving the following warning (many times over):
我正在使用自定义信号处理程序在自定义 java 守护程序中捕获 TERM、ABRT 和 INT 信号。我在代码中有这个处理程序,以便我可以向它发送 TERM 信号并通过 kill 命令优雅地关闭程序。信号处理程序现在可以工作,但是当我编译代码时,我收到以下警告(多次):
warning: sun.misc.SignalHandler is Sun proprietary API and may be removed in a future release
警告:sun.misc.SignalHandler 是 Sun 专有的 API,可能会在未来的版本中删除
while using these classes:
在使用这些类时:
import sun.misc.SignalHandler;
import sun.misc.Signal;
Is there a better way to send signals to a running JVM to initiate a shutdown of the main thread? I don't like having my code tied to this API when it could be removed in the future.
有没有更好的方法将信号发送到正在运行的 JVM 以启动主线程的关闭?我不喜欢将我的代码绑定到这个 API,因为它将来可能会被删除。
This code works on Solaris and HPUX today using 1.5.0_22 JVM. Any help or suggestions would be much appreciated. I used this document, from IBM, to develop the signal handler:
此代码目前在 Solaris 和 HPUX 上使用 1.5.0_22 JVM。任何帮助或建议将不胜感激。我使用 IBM 的这份文档来开发信号处理程序:
http://www.ibm.com/developerworks/java/library/i-signalhandling/
http://www.ibm.com/developerworks/java/library/i-signalhandling/
回答by ykaganovich
First of all, understand that this is just a standard warning for sun.misc package. They're letting you know that the API you're using is not a standard Java API. It doesn't mean that they're actively planning to remove this API in the future. http://java.sun.com/products/jdk/faq/faq-sun-packages.html
首先要明白这只是sun.misc包的标准警告。他们让您知道您使用的 API 不是标准的 Java API。这并不意味着他们正在积极计划在未来删除此 API。http://java.sun.com/products/jdk/faq/faq-sun-packages.html
As far as your question, it's not quite clear to me why the default way java process handles kill signals is not sufficient for you: How to stop java process gracefully?. If you need to add additional logic, you can add a shutdown hook.
就您的问题而言,我不太清楚为什么 java 进程处理终止信号的默认方式对您来说是不够的:如何优雅地停止 java 进程?. 如果需要添加额外的逻辑,可以添加一个关闭钩子。
Still, If you're looking for other ways to let your java process know it's time to exit, you can have it listen on a socket, or stdin, or a named pipe...
尽管如此,如果您正在寻找其他方法让您的 java 进程知道是时候退出了,您可以让它监听套接字、标准输入或命名管道......
You might also want to look into JVMTI
您可能还想研究JVMTI
回答by Zenil
You could do this via JMX. JMX is a standard set of apis that can be used to monitor and manage java applications.
您可以通过 JMX 执行此操作。JMX 是一组标准的 api,可用于监视和管理 java 应用程序。
Here are some links to get you started :
以下是一些帮助您入门的链接:
http://onjava.com/pub/a/onjava/2004/09/29/tigerjmx.html
http://onjava.com/pub/a/onjava/2004/09/29/tigerjmx.html
http://www.ibm.com/developerworks/java/library/j-jtp09196/index.html?ca=drs
http://www.ibm.com/developerworks/java/library/j-jtp09196/index.html?ca=drs
The main idea is this :
主要思想是这样的:
a) You will have a boolean variable, say isShutDownTrigerred. You will have a thread that will run a infinite loop, sleep for 2s, and keeps checking this variable value. When the value is true, you will execute code that will shutdown the application.
a) 您将有一个布尔变量,例如 isShutDownTrigerred。您将拥有一个将运行无限循环、休眠 2 秒并不断检查此变量值的线程。当值为 true 时,您将执行关闭应用程序的代码。
b) Then you write a mxbean (check the links above). This mxbean will be used to change the "isShutDownTrigerred" value to true. You can use a tool like jconsole /jManage to see and modify the mxbeans of a java application. As soon as the "isShutDownTriggered" is set to true, the above thread is going to know it and will execute the shutdown of the application
b) 然后你写了一个 mxbean(检查上面的链接)。此 mxbean 将用于将“isShutDownTrigerred”值更改为 true。您可以使用 jconsole /jManage 之类的工具来查看和修改 java 应用程序的 mxbean。一旦“isShutDownTriggered”设置为true,上面的线程就会知道并执行应用程序的关闭