从多个位置正确退出正在运行的程序的设计模式
我有一个用java编写的系统,其中有多个不同的对象,每个对象都使用不同的资源。有些具有与 activeMQ 队列的连接,有些具有网络连接,有些具有打开的文件。有些还包含正在运行的线程。
当这个系统中的任何地方发生致命错误时,我需要将其关闭并正确关闭所有资源并停止所有正在运行的线程。
当导致错误的对象需要启动关闭过程时,就会出现我的问题。该对象不知道其他具有打开文件等的对象。所以它基本上可以释放它所有的资源,仅此而已。
我正在寻找一种干净的方法来实现这一目标,而不会变得混乱并在系统中传递多个对象引用。
任何见解都值得赞赏。谢谢。
I have a system written in java where I have multiple distinct objects each with different resources in use. Some have connections to activeMQ queues, some have network connections and others have open files. Some also contain running threads.
When a fatal error occurs anywhere in this system, I need to shut it down and correctly close all resources and stop all running threads.
My problem arises when the object that caused the error needs to start the shutdown process. This object does not know about the other objects that have open files and so on. So it can basically release all its resources and that is it.
I am looking for a clean way to achieve this without getting messy and passing multiple object references around the system.
Any insight is appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个中心
Lifecycle
对象,应用程序中的所有其他对象都引用该对象,而该对象又引用所有其他对象。此外,这些对象中的每一个都应该实现一个通用接口,例如当其中一个对象需要启动有序关闭时,它可以调用
lifecycle.shutdown()
,而后者又可以调用object .onShutdown()
在所有向其注册的对象上,以便让这些对象有机会关闭其资源。这基本上就是观察者模式。
如果您使用依赖注入容器(例如 Spring),这种类型的东西是内置的 - 您的 bean 可以扩展某个接口,以便在容器关闭时收到通知。
Create a central
Lifecycle
object which all of these other objects in your application have a reference to, and which in turn has a reference to all of these other objects. In addition, each of these objects should implement a common interface such asWhen one of the objects needs to start an orderly shutdown, it can call
lifecycle.shutdown()
which can in turn callobject.onShutdown()
on all of the objects registered with it, in order to give these objects a chance to close their resources.This is basically the Observer pattern.
If you use a dependency-injection container such as Spring, this type of thing is built-in - your beans can extend a certain interface to be notified when the container is shutting down.
您也许可以使用 关闭挂钩 为此。在其中您可以通知所有相关对象,当然,这些对象需要在某个地方注册。
You may be able to use Shutdown hook for that. In it you can notify all related objects, which, of course, need to be registered somewhere.
java虚拟机允许注册
Shutdown Hooks
。您的数据库连接池、文件 IO 管理器、activeMQ 队列管理器都可以独立注册自己的关闭钩子,每个钩子都会干净地关闭所有资源。关闭钩子是一个线程,需要引用它负责关闭的资源管理器。当应用程序终止时,将执行该线程的 run 方法。您的应用程序可以在任何地方注册关闭钩子,因为
Runtime.getRuntime()
可作为静态调用使用,因此无需将其连接到需要它的应用程序区域(尽管建议这样做)您在创建每个资源管理器时注册此类关闭挂钩)。更多信息请点击这里。
http://download .oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
A java virtual machine allows the registration of
Shutdown Hooks
. Your database connection pool, your file IO manager, your activeMQ queues manager can all independently register their own shutdown hooks that each close all their resources cleanly.A shutdown hook is a Thread that requires a reference to the resource manager(s) it's responsible for shutting down. The
run
method of this thread will be executed when the application is terminated. Your application has access to register a shutdown hook anywhere asRuntime.getRuntime()
is available as a static call, so no need to wire it into the areas of the application that need it (although it is advisable that you register such shutdown hooks at the time of the creation of each resource manager).More information here.
http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)