Search code examples
javaakkaguavafuture

ListenableFuture - finish after message in cluster is received and time consuming code is executed


At first I just want to say that I'm new to akka and Futures. So be gentle :).

I have init method in some class which returns ListenableFuture<Boolean>. This method should execute some time consuming code in separate thread and create akka actor which is listening to some messages in akka cluster. Future returned by init method should be completed after this actor receives certain message AND that time consuming code is finished.

How can I achieve this using ListenableFuture from Guava?


Solution

  • I solved it like this. I created ListenableFuture containing time consuming code. At the end of this furute I created CountDownLatch and passed it (using creator) to akka actor. This listenable future was then blocked by countDownLatch.await(). Created actor with injected latch then listened to certain message in cluster. After consuming this certain message I called countDownLatch.countDown(). This unblocks future, where was await() called and future finished and returned value.