优雅地停止Java服务,使用NoHoup命令执行
我是在Java Spring-boot的初学者
当我通过
nohup Java -jar demo-0.0.1-snapshot.jar.jar
命令我的线程
有时在我停止我的服务器时,
杀死-9 ProcessID
,
因此我的运行线程数据丢失了。
每当服务器停止时,我想在数据库中维护线程标志。
我试图在遵循此教程春季启动关闭时,我试图实现上述情况。它可以正常工作,但它打破了我的安全性,因为任何人都可以使用此API停止我的服务器。
我需要一种优雅地终止服务器并调用上下文contestalcledevent
类的方法。
I am a beginner in java spring-boot
when I start the java server through
nohup java -jar demo-0.0.1-SNAPSHOT.jar
command my threads start their operations
After sometimes when I stop my server using
kill -9 processid
So my running thread data are lost.
I want to maintain a thread flag in the database whenever server stops.
I have tried to achieve the above scenario in following this tutorial Spring Boot Shutdown. It works properly but it breaks my security as anyone can use this API to stop my server.
I need a way to terminate my server gracefully and call ContextClosedEvent
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
kill -9
,则没有Spring Boot提供的优雅关闭支持。那就是sigkill
,无法捕获,阻止或忽略,因此您的应用程序当然不能对此做任何事情。使用
kill -sigterm
允许关闭挂钩在Java运行时执行。避免使用15和9之类的魔术数字;使用有意义的符号,例如
sigterm
。No graceful shutdown support provided by Spring Boot is going to work if you use
kill -9
. That isSIGKILL
, which can't be caught, blocked, or ignored, so of course your application can't do anything in response to it.Use
kill -SIGTERM
to allow shutdown hooks to execute in your Java runtime.Avoid magic numbers like 15 and 9; use meaningful symbols like
SIGTERM
instead.如果您使用的是2.3及以后的Spring Boot版本,则为优雅的关闭应添加到
application.yml
:from Java doc:
另一个选项是
立即
:其他资源
If you are using Spring Boot version from 2.3 and later, special configuration for graceful shutdown should be added to
application.yml
:From java doc:
Another option is
immediate
:Additional resource Shutdown Spring Boot Applications Gracefully