title: Continue processing after AsyncContext.complete()
body: <p>First of all I want to say that English is not my first language, so excuse me if I make some obvious mistakes or something is not clear enough. The question:</p>
  
  <p>Recently I've been moved to a new project where we are developing a Java EE application that provides some REST services via Jersey+Hibernate. Prior to this I only had experience with Java SE, but there was someone experienced already laying the foundations so I had my time to learn from his code and using Google and SO a lot. </p>
  
  <p>The thing is, among those REST services there is one that can take a lot of time to complete, and the team decided to implement it in a non-blocking way. We will define two services: with the first one the client sends the data to process, then we return an acknowledgement and start the processing while the client can continue with other things; and the second one allows the client to check later if their job is done.</p>
  
  <p>While investigating how to implement this the best way possible, my aforementioned colleague discovered the <code>AsyncServlet</code> feature of Servlets 3.0, and had a proof of concept implemented before I arrived, which later he evolved into a locally working (but very dirty) version of the service. He says he had to drop Jersey for this, since Servlets 3.0 weren't compatible with the version of Jersey we work with, and finally decided to implemented a plain servlet.</p>
  
  <p>At the end, he got something like this (I don't have the code right now since I'm at home and writing by memory, but I'll try to write it as clear as possible and try to fix any big mistakes tomorrow morning):</p>
  
  <p>A servlet which handles the new requests in <code>doPost()</code> and the checking in <code>doGet()</code>:</p>
  
  <pre><code>@WebServlet(asyncSupported = true)
  
  //...
  
  void doGet(HttpServletRequest req, HttpServletResponse res) {
  
      /* 
       * ...    
       * We query the previous "job request" here in the DB
       * ...
       */
  
  }
  
  void doPost(HttpServletRequest req, HttpServletResponse res) {
  
      /* 
       * ...
       * We convert the JSON request to an entity and then start the asynchronous 
       * "worker" thread
       * ...
       */
  
      AsyncContext ctx = req.startAsync(req, res);
      ctx.start(new WorkerThread(ctx, someOtherDataFromRequest);
  }
  </code></pre>
  
  <p>And a worker thread that implements <code>Runnable</code> and the first thing it does is to call <code>ctx.complete()</code> on the <code>AsyncContext</code> that was sent to him in the constructor. My colleague  reasoning is, if the worker notifies the parent right away he has completed, the parent can commit the response back to the client and then start his own processing with the other data passed to him in the constructor:</p>
  
  <pre><code>public class WorkerThread implements Runnable {
  
      public WorkerThread(AsyncContext ctx, SomeOtherData data){
  
          //...
  
      }
  
      public void run() {
          ctx.complete();
  
          // ... Now start doing the heavy processing with data
  
      }
  }
  </code></pre>
  
  <p>Well, as I said this works on his local test server (Tomcat 7), but a few days ago I was asked to clean his code and when ran on my machine which has a JBoss EAP 6.1, I found that it doesn't work as expected, since the parent servlet does not commit the response until the worker dies (we have different servers because the production machines are new and the higher ups haven't decided which server to install yet and changed their minds a bunch of times, bureaucracy...)</p>
  
  <p>I did a bunch of tries and I'm pretty sure I didn't remove any key element of the async processing while cleaning, as my version compiles and runs fine. At the end I got a test case where the worker just sleeps 10 seconds, then writes at the log; in Tomcat the response reaches the client almost instantly then at 10s the log is written; while in JBoss the client has to wait the full 10s to receive the response.</p>
  
  <p>Then, I started investigating the <code>AsyncServlet</code> feature, and I think he got the idea the wrong way, this feature seems to be directed to asynchronous internal processing and not as we want to use it, but I can't understand why it does work on his Tomcat. From the javadocs of the <code>complete()</code> method I understand the JBoss behavior is the correct one:</p>
  
  <blockquote>
    <p><em>If this method is called before the container-initiated dispatch that called startAsync has returned to the container, then the call will not take effect (and any invocations of AsyncListener#onComplete(AsyncEvent) will be delayed) <strong>until after the container-initiated dispatch has returned to the container</strong>.</em></p>
  </blockquote>
  
  <p>So, my question is if the <code>AsyncServlet</code> features are intended for our use case, and, if not, if there are any other cleaner ways to get our desired behavior (bonus points if they are compatible with Jersey). I'm thinking on just spawning a thread and not using the asyncontext at all, but sounds quite risky...</p>
  
  <p>Thanks and sorry for the wall of text</p>