Daniel Mikusa opened SPR-6642 and commented

Submitted for customer --> Liu, Yinwei David (Morgan Stanley)


Since Spring 3 introduces SmartLifecycle to auto-start components when ApplicationContext refresh and/or shutdown in a particular order. I think SmartLifecycle will gradually be used by those spring-powered framework to auto-start their components, and Spring already uses it in its core framework, e.g. SchedulerFactoryBean, SimpleMessageListenerContainer and JmsMessageEndpointManager. And also, I notice that all components choose Integer.MAX_VALUE (lowest priority) as their phase.

I think the question is how can people choose a correct phase when they develop their own component. Can Spring provide a recommendation in its documentation?

Let me give one use case: I have one application which has three SmartLifecycle components, HttpClient, DataSyncService, TCPServer. The start order of my application would be: 1. Start HttpClient 2. Start DataSyncService to sync data from an exteranl HTTP service via HttpClient, and then update its DB. 3. Start TCPServer to accept requests The issue is that both HttpClient and TCPServer are provided by 3rd party so they do not know which phase is the correct one, so they may have a same phase. The worse thing would be that TCPServer have a higher order than HttpClient/DataSyncService, that means TCPServer will get started before DataSyncService refreshes its data, and it can return some out-of-date data to the client while Spring is starting DataSyncService.

In order to avoid this issue, I think Spring framework is the right place to give some recommendations about the right phase for different compoents. It does not enforce people to use it, but it does give the idea about what kind of number people may choose for different components. For example, in our code, we give some recommand phases below so that people can choose a right phase for their own compoennt:

|Component|SmartLifecycle.getPhase()| |ServerTransports|3,000,000| |CacheManager|1,750,000| |Datasources|1,500,000| |Pool|1,250,000| |ClientTransports|1,000,000|

Is it possible that Spring can give the similiar recommandation in its doc so that all developers can get a general idea about which phase can be use?


Affects: 3.0 GA

Comment From: jhoeller

Even in our latest revision, we keep using Integer.MAX_VALUE (now known as DEFAULT_PHASE) for our SmartLifecycle components. I don't foresee any further guidance from our side, concrete multi-phase values are left up to the application.