java - Design Pattern to correctly exit a running program from multiple locations -


i have system written in java have multiple distinct objects each different resources in use. have connections activemq queues, have network connections , others have open files. contain running threads.

when fatal error occurs anywhere in system, need shut down , correctly close resources , stop running threads.

my problem arises when object caused error needs start shutdown process. object not know other objects have open files , on. can release resources , it.

i looking clean way achieve without getting messy , passing multiple object references around system.

any insight appreciated. thank you.

create central lifecycle object of these other objects in application have reference to, , in turn has reference of these other objects. in addition, each of these objects should implement common interface such

public interface shutdownlistener {    void onshutdown(); } 

when 1 of objects needs start orderly shutdown, can call lifecycle.shutdown() can in turn call object.onshutdown() on of objects registered it, in order give these objects chance close resources.

this the observer pattern.

if use dependency-injection container such spring, type of thing built-in - beans can extend interface notified when container shutting down.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -