title: Using Spring 4.0's new ListenableFuture With Callbacks - Odd Results
body: <p>I have a web app that takes an array of IDs, queries an external webservice for each ID one at a time and publish each result as it arrives to a WebSocket client via a STOMP broker.  I can get this to work using simple Futures, but I'm trying to use Spring 4's new ListenableFutures and provide a callback.  </p>
  
  <p>The working code uses a ThreadPoolTaskExecutor that is defined in my root config.  I have a class called "SosQuery" with a method called "test" that is annotated with @Async and returns an AsyncResult.  Here is my working code being called from a root context service class:</p>
  
  <pre><code>@Override
      public void test(String[] oids) throws Exception {
          List&lt;Future&lt;String&gt;&gt; futures = new ArrayList&lt;Future&lt;String&gt;&gt;();
  
          for (String oid : oids) {
              futures.add(sosQuery.test(oid));
          }
  
          while (!futures.isEmpty()) {
              List&lt;Future&lt;String&gt;&gt; done = new ArrayList&lt;Future&lt;String&gt;&gt;();
              for (Future&lt;String&gt; future : futures) {
                  if (future.isDone()) {
                      messagingTemplate.convertAndSendToUser("me", "/queue/observation", future.get());
                      done.add(future);
                  }
              }
              futures.removeAll(done);
          }
      }
  </code></pre>
  
  <p>This works fine and I see the responses arriving in my client.  I modified the SosQuery method that is defined with the @Async annotation to simply return "String", and created a SimpleAsyncTaskExecutor in my root config.  Here is the modified method to use ListenableFuture:</p>
  
  <pre><code> @Override
      public void test(String[] oids) throws Exception {
          for (final String oid : oids) {
                ListenableFuture&lt;String&gt; task = asyncTaskExecutor.submitListenable(new Callable&lt;String&gt;(){
                  @Override
                  public String call() throws Exception {
                      String result = sosQuery.test(oid);
                      logger.debug("result for sosQuery: " + result);
                      return result;
                  }
              });
  
              task.addCallback(new ListenableFutureCallback&lt;String&gt;() {
  
                  @Override
                  public void onSuccess(String result){
                      if (result == null){
                          result = "ITS NULL";
                      }
                      messagingTemplate.convertAndSendToUser("me", "/queue/observation", result);
                  }
  
                  @Override
                  public void onFailure(Throwable t){
                      logger.error("Error executing callback.", t);
                  }
              });
          }
      }
  </code></pre>
  
  <p>I'm seeing weird behavior... when I deploy in debug mode, I can see that the call() method is being executed and the result is being built from the SosQuery class properly, however my logger statement never appears in the logs.  Immediately aftewards, the onSuccess method executes, but the result String is null.  </p>
  
  <p>The onFailure method never gets called and there is absolutely nothing distinctive in the logs.  Documentation for using the ListableFutures is scarce and tightly coupled to the AsyncRestTemplate, but little exists for just creating your own tasks.  Does anybody have any idea what I might be doing wrong?</p>