title: How to handle Java futures with Akka actors
body: <p>I have a layered architecture in a Java web application. The UI layer is just Java, services are typed Akka actors and external service calls (WS, DB etc.) are wrapped in Hystrix commands.</p>
  
  <p>THe UI calls the service and the service returns an Akka future. It's an Akka future because I want to make UI coding simpler with the onComplete and onFailure callbacks that Akka futures provide. The service then creates the future that does some mapping etc. and wraps a call to a HystrixCommand that returns a Java future.</p>
  
  <p>So in pseudocode:</p>
  
  <p>UI</p>
  
  <pre><code>AkkaFuture future = service.getSomeData();
  </code></pre>
  
  <p>Service</p>
  
  <pre><code>public AkkaFuture getSomeData() {
      return future {
          JavaFuture future = new HystrixCommand(mapSomeData()).queue()
          //what to do here, currently just return future.get()
      }
  }
  </code></pre>
  
  <p><strong>The problem is</strong> that I would like to free up the thread the service actor is using and just tie up the threads that Hystrix uses. But the java future prevents that because I have to block on it's completion. The only option I can think of (which I'm not sure I like) is to poll the Java future(s) constantly and complete the Akka future when the Java future finishes.</p>
  
  <p>Note: the question isn't really related to Hystrix per se, but I decided to mention it if somebody comes up with a solution specifically related to Hystrix.</p>