consciouscode.seedling.tree
Class ShutdownNode

java.lang.Object
  extended by consciouscode.seedling.tree.GenericNode
      extended by consciouscode.seedling.tree.ShutdownNode
All Implemented Interfaces:
LocatableNode, ServiceNode

public class ShutdownNode
extends GenericNode
implements ServiceNode

A seedling node that handles clean shutdown of the JVM and of the Seedling itself.

Nodes that need special shutdown handling can register themselves with the ShutdownNode. This is cleaner and safer than using a standard shutdown hook, since the handlers will be called before JVM shutdown if the seedling is stopped programmatically.


Constructor Summary
ShutdownNode()
           
 
Method Summary
 void addShutdownHandler(Runnable handler)
          Add a handler to be run at shutdown.
 void removeShutdownHandler(Runnable handler)
          Remove a shutdown handler.
 void runShutdownHandlers()
          Run the registered shutdown handlers, but do not exit the JVM.
 void shutdown(int exitCode)
          Run the registered shutdown handlers, then exit the JVM.
 void startService()
          Starts a Seedling service.
 void stopService()
          Stops a Seedling service.
 
Methods inherited from class consciouscode.seedling.tree.GenericNode
getLocalRoot, getLog, getNodeLocation, getNodeName, getParentBranch, isLoggingDebug, nodeInstalled, setLog, setLoggingDebug
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ShutdownNode

public ShutdownNode()
Method Detail

addShutdownHandler

public void addShutdownHandler(Runnable handler)
Add a handler to be run at shutdown. Note that handlers are run in the reverse order that they were added.

If the registered handlers are currently running in another thread, this method will block until they have run to completion.

Parameters:
handler - the item to add to the internal handler list. If the handler is already in the list, this method does nothing.

removeShutdownHandler

public void removeShutdownHandler(Runnable handler)
Remove a shutdown handler.

If the registered handlers are currently running in another thread, this method will block until they have run to completion.

Parameters:
handler - the item to remove from the internal handler list. If the handler is not in the list, this method does nothing.

shutdown

public void shutdown(int exitCode)
Run the registered shutdown handlers, then exit the JVM.

Parameters:
exitCode - the value passed to System.exit(int).
Throws:
IllegalStateException - if this method is called from a running handler.

runShutdownHandlers

public void runShutdownHandlers()
Run the registered shutdown handlers, but do not exit the JVM. The internal handler list is then cleared.

If the registered handlers are currently running in another thread, this method will block until they have run to completion.

If the registered handlers are currently running in thisthread, this method will do nothing.


startService

public void startService()
Description copied from interface: ServiceNode
Starts a Seedling service. This is called by the Seedling runtime after the node has been installed and all properties have been set to their configured values. Any exceptions thrown by an implementation of this method are handled by the Seedling. Instances of RuntimeException are propagated to the application, but any other kind of exception is wrapped in a ServiceException and rethrown.

Specified by:
startService in interface ServiceNode

stopService

public void stopService()
Description copied from interface: ServiceNode
Stops a Seedling service. This is called by the Seedling runtime as the node is uninstalled, either explicitly or by the Seedling itself being stopped.

Implementations of this method must not do anything that may change the state of the parent Seedling. In particular, it cannot call any overload of the following methods: BranchNode.getNode(java.lang.String), BranchNode.getChild(java.lang.String), GrowingBranch.installChild(java.lang.String, java.lang.Object), GrowingBranch.uninstallChild(java.lang.String).

Furthermore, this method may be called from within a JVM shutdown hook, which implies that it must work promptly and carefully.

Specified by:
stopService in interface ServiceNode
See Also:
Runtime.addShutdownHook(java.lang.Thread)


Copyright © 2001–2012 Todd V. Jonker. All Rights Reserved.