Below analysis is for spring-flex 1.0.3.RELEASE and BlazeDS 4.0.0.14931
1. On the main 'startup' thread during application startup, the <flex:message-broker id="_messageBroker"/> tag creates and initializes the BlazeDS MessageBroker. As a side-effect of creating the MessageBroker, several ThreadLocals for the startup thread are created:[ DelegatingServletConfig, SystemSettings, MessageBroker, SerializationContext ]
2. The problem is that these thread locals on the startup are never cleared. Thus when Tomcat shuts the application down, it complains about the thread leak (see original error msg in this post).
3. The fix is fairly simple: in my 'blazeds-config.xml' file used for the blazeds servlet configuration, I added:
Code:
<!-- CleanupBlazeDSThreadLocalsOnStartup uses @PostConstruct in this servlet context -->
<context:annotation-config/>
and
<!--
Spring flex does NOT cleanup BlazeDS thread locals created from main initialization thread,
which results in Tomcat reporting a memory leak at shutdown.
-->
<bean class="com.abinitio.dashboard.dispatch.flex.utils.CleanupBlazeDSThreadLocalsOnStartup"
depends-on="_messageBroker"/>
The CleanupBlazeDSThreadLocalsOnStartup bean is also constructed on the 'startup' thread, and thus can clear the corresponding thread locals that are left over from the message broker creation :
Code:
@PostConstruct
public void afterPropertiesSet()
{
// Clear the thread locals used by the 'main' startup thread.
// Otherwise get thread leak messages from Tomcat on application shutdown.
FlexContext.clearThreadLocalObjects();
// Clear other thread local objects on the 'main' initialization thread
SerializationContext.clearThreadLocalObjects();
TypeMarshallingContext.clearThreadLocalObjects();
}
This worked - after doing the above, Tomcat no longer complains about BlazeDS-related memory leaks.