title: Please wait page in Spring MVC + Apache Tiles
body: <p>I'm using Spring MVC 3 + Tiles for a webapp. I have a slow operation, and I'd like a please wait page.</p>
  
  <p>There are two main approaches to please wait pages, that I know of:</p>
  
  <ol>
  <li>Long-lived requests: render and flush the "please wait" bit of the page, but don't complete the request until the action has finished, at which point you can stream out the rest of the response with some javascript to redirect away or update the page.</li>
  <li>Return immediately, and start processing on a background thread. The client polls the server (in javascript, or via page refreshes), and redirects away when the background thread finishes.</li>
  </ol>
  
  <p>(1) is nice as it keeps the action all single-threaded, but doesn't seem possible with Tiles, as each JSP must complete rendering in full before the page is assembled and returned to the client.</p>
  
  <p>So I've started implementing (2). In my implementation, the first request starts the operation on a background thread, using Spring's <code>@Async</code> annotation, which returns a <code>Future&lt;Result&gt;</code>. It then returns a "please wait" page to the user, which refreshes every few seconds.</p>
  
  <p>When the please wait page is refreshed, the controller needs to check on the progress of the background thread. What is the best way of doing this?</p>
  
  <ol>
  <li>If I put the <code>Future</code> object in the Session directly, then the poll request threads can pull it out and check on the thread's progress. However, doesn't this mean my Sessions are not serializable, so my app can't be deployed with more than one web server (without requiring sticky sessions)?</li>
  <li>I could put some kind of status flag in the Session, and have the background thread update the Session when it is finished. I'm very concerned that passing an HttpSession object to a non-request thread will result in hard to debug errors. Is this allowed? Can anyone cite any documentation either way? It works fine when the sessions are in-memory, of course, but what if the sessions are stored in a database? What if I have more than one web server?</li>
  <li>I could put some kind of status flag in my database, keyed on the session id, or some other aspect of the slow operation. It seems weird to have session data in my domain database, and not in the session, but at least I know the database is thread-safe.</li>
  <li>Is there another option I have missed?</li>
  </ol>