I have a Spring 3.0 WebMVC application running on Tomcat 7. On application startup I kick off a background thread to load an in-memory cache with records from a DB. This thread typically takes over an hour to load all the data from the DB. In the same application I have a @Controller annotated class which exposes a REST interface by which clients can get objects from the loaded cache.
One of our requirements is that any REST requests that are made prior to the data load being finished will return a SERVICE_UNAVAILABLE (503) HTTP code to the client immediately. To accomplish this I set up a simple check of a boolean flag that each request method in the controller makes before doing any work. If the value is false the method should return immediately with a 503 code. The loader thread will set the flag to true once it is completed with the load to allow the request methods to function normally.
The problem is that the background thread seems to be causing all HTTP requests sent to my controller to timeout after 30 seconds instead of hitting the flag and returning a 503 code immediately to the client. I'm not an expert in Tomcat threading issues and I wonder if I'm doing something wrong when creating my long running background thread? I am basically using the "implements Runnable" method to create the thread as described in this question and starting the thread on the containing bean initialization. Is there a better way to kick off a long running background thread inside Tomcat that doesn't interfere with the request handling? Or is there something else that I'm missing?