简体   繁体   English

在NetBeans IDE中运行Shutdown Hook

[英]Running Shutdown Hook in NetBeans IDE

I'm developing an application that will run as a service. 我正在开发一个将作为服务运行的应用程序。 I have added a shutdown hook to the program to do some file cleanup. 我已经为程序添加了一个关闭钩子来进行一些文件清理。 When I install the program as a service (on Linux as a daemon or as a Windows service) the shutdown hook executes correctly. 当我将程序作为服务安装时(在Linux上作为守护程序或作为Windows服务),关闭挂钩正确执行。 However, when running in the IDE the shutdown hook is never executed. 但是,在IDE中运行时,永远不会执行关闭挂钩。 Is there a way to stop the running process in the IDE and have the shutdown hook execute? 有没有办法在IDE中停止正在运行的进程并执行关闭钩子?

Thanks, Pablo 谢谢,巴勃罗

Unfortunately there is no way to do this within the current Netbeans system,since the kill methods for both types of app testing (Debug and Normal) both, well kill the process, giving it no chance to cleanup. 不幸的是,在当前的Netbeans系统中没有办法做到这一点,因为两种类型的应用程序测试(Debug和Normal)的kill方法都很好地杀死了进程,没有机会进行清理。 Depending on the architecture of your app you may be able to add a System.exit(0) call, but other than that you are stuck with opening it up in the console. 根据应用程序的体系结构,您可以添加System.exit(0)调用,但除此之外,您仍然无法在控制台中打开它。

I disagree. 我不同意。 A sigkill in unix or terminate process in windows by definition will kill the running process without giving any ability for the application to catch these events. 在Unix系统中的SIGKILL或定义终止进程的窗口将杀死运行过程中没有给予任何捕获这些事件的能力的应用程序。 This is necessary to terminate an unresponsive process. 这对于终止无响应的过程是必要的。 These signals should only be used when a process does not respond to a siginit (ctrl c) or a sigterm or end task in windows. 仅当进程不响应siginit(ctrl c)或sigterm或end任务时,才应使用这些信号。 NetBeans seems to be sending a sigkill rather than siginit or sigterm. NetBeans似乎发送了一个sigkill而不是siginit或sigterm。 As far as I'm concerned this is bad practice. 就我而言,这是不好的做法。

There should be a secondary option to kill the process however the primary end process button should be to send siginit or sigterm. 应该有一个辅助选项来终止进程,但主要进程按钮应该是发送siginit或sigterm。 Applications should anticipate the user hitting ctrl c or end task on their application and as a best effort cleanly close files/sockets and save persistent/state data. 应用程序应该预期用户在其应用程序上执行ctrl c或end任务,并尽可能干净地关闭files/sockets并保存persistent/state数据。 An application should not anticipate a sigkill or terminate process but rather be developed in such a way that a sigkill or terminate process are not necessary. 应用程序不应该预期sigkill或终止进程,而是以不需要sigkill或终止进程的方式开发。

By NetBeans using these methods as the only means to terminate the application from the IDE is erroneous. NetBeans使用这些方法作为从IDE终止应用程序的唯一方法是错误的。

Nope. 不。 Don't cleanup your program using shutdown hooks. 不要使用shutdown hooks清理程序。 From the docs ... 来自文档 ......

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. 在极少数情况下,虚拟机可能会中止,即停止运行而不会干净地关闭。 This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. 当虚拟机在外部终止时会发生这种情况,例如Unix上的SIGKILL信号或Microsoft Windows上的TerminateProcess调用。 The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. 如果本机方法因例如破坏内部数据结构或尝试访问不存在的内存而出错,则虚拟机也可能中止。 If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run. 如果虚拟机中止,则无法保证是否将运行任何关闭挂钩。

When the contract says that there is no guarantee that your shutdown hooks will always be called, it is not advisable to use them for critical operations. 当合同声明无法保证始终会调用您的关闭挂钩时,建议不要将它们用于关键操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM