优雅地停止Java服务,使用NoHoup命令执行

发布于 2025-01-25 23:17:34 字数 467 浏览 3 评论 0原文

我是在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2025-02-01 23:17:34

如果您使用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 is SIGKILL, 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.

梨涡少年 2025-02-01 23:17:34

如果您使用的是2.3及以后的Spring Boot版本,则为优雅的关闭应添加到application.yml

server:
  shutdown: graceful

from Java doc:

Web服务器应支持优雅的关闭,使主动请求有时间完成

另一个选项是立即

Web服务器应立即关闭

其他资源

If you are using Spring Boot version from 2.3 and later, special configuration for graceful shutdown should be added to application.yml:

server:
  shutdown: graceful

From java doc:

The WebServer should support graceful shutdown, allowing active requests time to complete

Another option is immediate:

The WebServer should shut down immediately

Additional resource Shutdown Spring Boot Applications Gracefully

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文