为 Eclipse RCP 应用程序添加 Shutdown Hook 的正确方法是什么?
我有一个 RCP 应用程序,它使用与内存数据库的连接。 有一种情况是,当关闭窗口时,应用程序被终止,而没有给它机会关闭与数据库的连接。
我做了一些研究,似乎添加 Shutdown 挂钩是检测此事件并在 Java 应用程序中进行清理的最佳方法。 但是,如果您有 RCP 应用程序(可能打开了多个编辑器),处理此问题的正确方法是什么?
I have an RCP application that uses a connection to a in-memory database. There is one circumstance that, when shutting down windows, the application is killed without giving it a chance to close the connection to the database.
I researched a little and it seems that adding a Shutdown hook is the best way to detect this event and do cleanup in a Java application. However, what is the correct way to do process this if you have an RCP application, possibly with multiple editors open?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:此博客条目建议对关闭挂钩执行以下实现:
(所以不完全是您的场景,但实现仍然很有趣,因为它展示了如何在 UI 线程中运行它)
正如您所说,它是在 IApplication 中设置的:
Note: this blog entry suggests the following implementation for the shutdown hook:
(so not exactly your scenario, but the implementation is still interesting in that it shows how to run it within the UI thread)
It is set, as you said, in the IApplication:
您应该重写扩展 WorkbenachAdvisor 的类上的 preShutdown 方法。 返回 false 以停止关闭过程,或返回 true 以继续。
You should override the preShutdown method on your class that extends WorkbenachAdvisor. Return false to halt the shutdown process or true to continue.
在实际启动 RCP 应用程序之前,我尝试了从 IApplication 实现者 start() 方法执行以下代码:
其中 cleanup() 关闭与数据库的连接。 如果有任何文档打开,关闭应要求用户保存。
I tried the following code, that I execute from my IApplication implementor start() method, before the RCP application is actually launched:
Where cleanup() closes the connection to the database. Close should ask the users to save if there is any documents open.