Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5036297
Votes 8
Synopsis Enhancements for the Shutdown Hooks API
Category java:classes_lang
Reported Against 1.4.2
Release Fixed
State 3-Accepted, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 22-APR-2004
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) 
======================================================================
Work Around
N/A
Evaluation
N/A
Comments
  
  Include a link with my name & email   

Submitted On 21-MAY-2004
CurtCox
See also:
http://weblogs.java.net/pub/wlg/1275


Submitted On 28-AUG-2007
uuklanger
This issues affects  SystemTray and TrayIcon.  When a user logs off or shuts down, the java process is just killed in Windows.  Under linux the shutdown hook works.  But it does not work consistently under all OS.  

Please consider this as a patch to SDK 6.



PLEASE NOTE: JDK6 is formerly known as Project Mustang