向正在运行的 JVM 发送信号

2022-01-16 00:00:00 signals jvm java

我正在使用自定义信号处理程序在自定义 Java 守护程序中捕获 TERM、ABRT 和 INT 信号.我在代码中有这个处理程序,以便我可以向它发送 TERM 信号并通过 kill 命令优雅地关闭程序.信号处理程序现在可以工作,但是当我编译代码时,我收到以下警告(多次):

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):

警告:sun.misc.SignalHandler 是 Sun 专有 API,可能会在未来的版本中删除

在使用这些类时:

import sun.misc.SignalHandler;
import sun.misc.Signal;

有没有更好的方法向正在运行的 JVM 发送信号以启动主线程的关闭?我不喜欢将我的代码绑定到这个 API,因为它将来可能会被删除.

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.

此代码现在可以在 Solaris 和 HPUX 上使用 1.5.0_22 JVM.任何帮助或建议将不胜感激.我使用 IBM 的这份文档来开发信号处理程序:

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:

http://www.ibm.com/developerworks/java/library/i-信号处理/

推荐答案

首先要明白这只是sun.misc包的标准警告.他们让您知道您使用的 API 不是标准的 Java API.这并不意味着他们正在积极计划将来删除此 API.http://java.sun.com/products/jdk/常见问题解答/faq-sun-packages.html

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

就您的问题而言,我不太清楚为什么 java 进程处理终止信号的默认方式对您来说不够:如何优雅地停止java进程?.如果需要添加额外的逻辑,可以添加 关机钩子.

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 进程知道该退出,您可以让它在套接字、标准输入或命名管道上侦听...

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...

您可能还想查看 JVMTI

相关文章