Thread management and the Spring Framework
Use the information in the following sections to avoid potential problems with unmanaged threads.
Unmanaged threads
Do not use a scenario that can create unmanaged threads, for the following reasons:
- The application server does not recognize unmanaged threads.
- Unmanaged threads do not have access to Java EE contextual information.
- Unmanaged threads can use resources without being monitored by the application server.
- Unmanaged threads can adversely affect application server functions such as shutting down gracefully or recovering resources from failure.
- An administrator cannot control the number of unmanaged threads or their use of resources.
The following scenarios are examples of Spring Framework scenarios to avoid:
- registerShutdownHook
Avoid using the Spring Framework AbstractApplicationContext class and its subclasses. These classes include the public method registerShutdownHook, which creates a thread and registers it with the JVM to run at shutdown to close the application context. As an alternative, an application can use the lifecycle notices that it receives from the application server container to call the close method explicitly on the application context.
- WeakReferenceMonitor
The Spring Framework provides convenience classes for simplified development of EJB components. However, these convenience classes spawn off an unmanaged thread that WeakReferenceMonitor object uses for cleanup.
Thread pooling
WebSphere Application Server supports the use of the Spring Framework WorkManagerTaskExecutor class to run work asynchronously.
The WorkManagerTaskExecutor class uses thread pools managed by the application server, and delegates to a configured WorkManager instance. For information about configuring a work manager, see the related topics.
Do not use other TaskExecutor classes provided with the Spring Framework, because they might start unmanaged threads.
We can use the JNDI name of the configured work manager as a workManagerName property to define a WorkManagerTaskExecutor instance in the Spring configuration file. The following example uses the JNDI name of the DefaultWorkManager in the application server, that is, wm/default:
<bean id="myTaskExecutor" class="org.springframework.scheduling.commonj.WorkManagerTaskExecutor"> <property name="workManagerName" value="wm/default" /> </bean>
Scheduling
Use the CommonJ WorkManager scheduling package in the Spring Framework to work with threads managed by the application server. Avoid using other packages, such as the Quartz scheduler, or the Timer in the Java SE Development Kit (JDK), because they can start unmanaged threads.
Configure work managers Spring Documentation