title: Async Spring Boot using Kotlin not working
body: <p>I'm trying to create a Spring Service that performs an operation asynchronously and returns a <code>ListenableFuture</code>. I want the failure callback to be triggered when the operation fails - my attempt to do this is to use <code>AsyncResult.forExecutionException</code> as seen below:</p>
  
  <pre><code>@Service
  open class UserClientService {
  
      @Async
      fun fetchUser(email: String): ListenableFuture&lt;User&gt; {
          val uri = buildUri(email)
          val headers = buildHeaders()
          try {
              val result = restTemplate.exchange(uri, HttpMethod.GET, HttpEntity&lt;Any&gt;(headers), User::class.java)
              return AsyncResult.forValue(result.body)
          } catch (e: RestClientException) {
              return AsyncResult.forExecutionException(e)
          }
      }
  }
  </code></pre>
  
  <p>The entry-point:</p>
  
  <pre><code>@SpringBootApplication
  @EnableAsync
  open class UserProxyApplication
  
  fun main(args: Array&lt;String&gt;) {
      SpringApplication.run(UserProxyApplication::class.java, *args)
  } 
  </code></pre>
  
  <p>The Spring RestController implementation is as follows:</p>
  
  <pre><code>@RestController
  @RequestMapping("/users")
  class UserController @Autowired constructor(
          val client: UserClientService
  ) {
      @RequestMapping(method = arrayOf(RequestMethod.GET))
      fun getUser(@RequestParam(value = "email") email: String): DeferredResult&lt;ResponseEntity&lt;User&gt;&gt; {
          val result = DeferredResult&lt;ResponseEntity&lt;User&gt;&gt;(TimeUnit.SECONDS.toMillis(10))
          client.fetchUser(email).addCallback(
                  { success -&gt; result.setResult(ResponseEntity.ok(success)) },
                  { failure -&gt; result.setResult(ResponseEntity(HttpStatus.NOT_FOUND)) }
          )
          return result;
      }
  }
  </code></pre>
  
  <p><strong>Problem</strong> is that the failure callback in the <code>UserController</code> is never triggered when an exception is thrown in the <code>UserClientService</code> REST call. Instead, the success callback is triggered with <code>success</code> argument being <code>null</code>. </p>
  
  <p>In Kotlin, I can check if success is null by using <code>success!!</code> - this throws an exception that then <em>does</em> trigger the failure callback with <code>failure</code> argument being the NPE.</p>
  
  <p><strong>Question</strong> is how can I trigger the failure callback in the <code>UserController</code> when an exception has occurred in the <code>UserClientService</code>?</p>
  
  <p><strong>Update A</strong> it seems that everything is executed on the same thread "http-nio-8080-exec-XXX" regardless of whether I use <code>@Async</code> or not -- see comments.</p>