|
Description
|
A DESCRIPTION OF THE REQUEST :
There are two primary weaknesses in the Shutdown Hook API:
1. Shutdown reason - no shutdown reason information is ever available
2. Hook ordering - there is no standard meachanism for ordering hooks
Both limitations are explained in "Design of the Shutdown Hooks API"
http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html
This request is motivated by the effort required to use the existing
Shutdown Hook API for cleanup of a large complex desktop application.
The limitations of the existing API are amplified by issues such as:
java.io.File.deleteOnExit does not work on open files (win32)
http://developer.java.sun.com/developer/bugParade/bugs/4171239.html
(fs) Add unmap method to MappedByteBuffer
http://developer.java.sun.com/developer/bugParade/bugs/4724038.html
The bugs are only examples. When performing shutdown
code:
1. The circumstances of the shutdown are important.
2. Order usually matters.
3. Native resources make everything worse.
JUSTIFICATION :
It is entirely possible for an application or library to provide these facilities, there are disadvantages to this approach.
1. Shutdown reason - an additional shutdown hook API could be added using native mechanisms. Applications written to the new API would suffer all of the portability and maintainability disadvantages of applications written to any other nonstandard native API. The design document overstates the complexity involving shutdown reason when it states that it is "impossible to generalize such information in a portable way". An example API is included in this RFE.
2. Hook ordering - shutdown hook ordering is left entirely up to the application. This has a number of disadvantages. Equivalent hook ordering logic must be written over and over by different individuals and organizations. More often, adequate shutdown logic is simply never written. Worse yet, for any pair of applications or libraries with potential shutdown hook ordering issues, one must be coded with the hooks of the other in mind.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Consider the following as a possible direction:
/**
Why is this shutdown happening? This class represents state information
about a Java shutdown. This is only a sample API to show that it is possible
to present considerable information in a platform independent way.
*/
public class ShutdownReason {
/**
Returns Boolean.TRUE, if the shutdown was requested by the user,
Boolean.FALSE if it wasn't requested by the user, or null if the
answer to this question is unknown.
*/
Boolean isUserRequested();
/**
Returns Boolean.TRUE, if the shutdown for the entire aggregate,
Boolean.FALSE if it is only for this Isolate, or null if the
answer to this question is unknown.
*/
Boolean isAggregateWide();
/**
Returns Boolean.TRUE, if the shutdown for the entire system (box),
Boolean.FALSE if it is limited to this Isolate or Aggregate,
or null if the answer to this question is unknown.
*/
Boolean isSytemWide();
/**
Returns Boolean.TRUE, if the shutdown is being forced to terminate
rapidly, Boolean.FALSE if it is not exiting under time pressure
or null if the answer to this question is unknown.
*/
Boolean isForced();
}
/**
A task to be performed at system shutdown.
A ShutdownHook to close a database might be created as follows:
<pre>
ShutdownHook hook = Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { database.close(); }
});
</pre>
This API only adresses hook ordering. Other things to consider when
developing a better API are:
<ol>
<li> Hooks can often be grouped into sets where every element in one set
should be done before any element in another set. Makeing these sets
explicit can speed execution, and make the code easier to understand.
<li> Hooks often require interaction with native resources that may have
no explicit cleanup API. PhantomReferences can be useful for such cleanup.
<li> Some shutdown activity should only be performed at shutdown. Other
shutdown activity can be performed as soon as some condition is met, but
must be performed before shutdown is complete. There should be a way
to integrate these two types.
</ol>
*/
public class ShutdownHook {
/**
Make this hook a prerequisite for the given hook.
@throws CyclicDependencyException if this would result in a shutdown
ordering that cannot be met.
*/
public void doBefore(ShutdownHook hook);
/**
Make the given hook a prerequisite for this one.
@throws CyclicDependencyException if this would result in a shutdown
ordering that cannot be met.
*/
public void doBefore(ShutdownHook hook);
}
ACTUAL -
The current Shutdown API is currently limited as detailed by:
http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html
CUSTOMER SUBMITTED WORKAROUND :
Implement a solution like the one specified in the "Expected Behavior" portion of this RFE.
(Incident Review ID: 232171)
======================================================================
|