1. If an MBean server is detected, do not register a shutdown hook.2. Else, register a shutdown hook.

To make things more sophisticated for cases where an MBean server may not control shutdown or a non-MBean server environment may control shutdown (such as a JBoss-MC standalone environment) there is a config parameter:

<!--
Available options:
DEFAULT (this is selected if this config element is omitted)
REGISTER (forces registering the hook, even if an MBean server is detected)
DONT_REGISTER (does not register a hook, even if an MBean server is not detected)
-->
<attribute name="ShutdownHookBehaviour">DEFAULT</attribute>

Thinking about it a bit more, and particularly about the comment in the user forum thread to:

1. remove any shutdown hooks in cache.stop() if this is called externally/manually2. add some intelligence in cache.stop() to detect if it is being called by the shutdown hook and don't remove the shutdown hook in this case

With the above, do we then need any sort of manual override regarding the registration of shutdown hooks? Can we not then make do with *always* registering a shutdown hook, and then have the hook unregister if cache.stop() is called manually, or by an MBean server/microcontainer?

Re: always adding the hook, no that will break the AS. There is no way to specify ordering of shutdown hook execution. If JBC's own hook executes first, the proper undeployment ordering is not followed. For example, a clustered webapp should be undeployed before the cache is. A JBC hook executing first would break this, leading to all sorts of ugliness as the AS shutdown hook executes (this is what was happening before we put CR2 in AS 5.)