Processing Future onSuccess in response from Akka Aker

This is the code that works. He sends a message to the actor (Greeter) and waits for a response. But it blocks the current thread.

public class Future1Blocking { public static void main(String[] args) throws Exception { ActorSystem system = ActorSystem.create("system"); final ActorRef actorRef = system.actorOf(Props.create(Greeter.class), "greeter"); Timeout timeout = new Timeout(Duration.create(5, "seconds")); Future<Object> future = Patterns.ask(actorRef, Greeter.Msg.GREET, timeout); // this blocks current running thread Greeter.Msg result = (Greeter.Msg) Await.result(future, timeout.duration()); System.out.println(result); } } 

What is the possible way to use my future.onSuccess example to get the result without blocking the current calling thread?

+6
source share
1 answer

Ahh. it was easy (sorry).

 future.onSuccess(new PrintResult<Object>(), system.dispatcher()); 

Where:

 public final class PrintResult<T> extends OnSuccess<T> { @Override public final void onSuccess(T t) { System.out.println(t); } } 
+10
source

Source: https://habr.com/ru/post/945275/


All Articles