2013-08-29

we are facing an issue with initializing our cache at server startup or application deployment. Initializing the cache involves

Querying a database to get the list of items

Making an rmi call for each item

Listening to the data on a JMS queue/topic

Constructing the cache

This initialization process is in startup code. All this is taking lot of time due to which the deployment is taking lot of time or server start time is increasing.

So what I proposed is to create a thread in the startup and run the initialization code in it. I wrote a sample application to demonstrate it.

It involves a ServletContextListener, a filter. In the listener I am creating a new thread in which the HeavyProcess will run. When it finishes an event will be fired which the filter will be listening. On receiving the event the filter will allow incoming http requests. Until then the filter redirects all clients to a default page which shows a message that the application is initializing.

I presented this approach and few concerns were raised.

We should not ideally create a thread because handling the thread will be difficult.

My question is why cant we create a thread like these in web applications.

If this is not good, then what is the best approach?

Show more