1akka中的future模式有两种,一种阻塞型,一种非阻塞型
阻塞:
Futuref= Patterns.ask(actor,msg,1500);
int re=(int)Await.result(f,timeout);
非阻塞:
Futuref= Patterns.ask(actor,msg,1500);
Patterns.pipe(f,system.dispatcher()).to(actor1)
//即将结果转移到另一个actor中进行处理
package akka;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.pattern.Patterns;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.Future;
public class future {
public static class print extends UntypedActor{
@Override
public void onReceive(Object o) throws Throwable {
if(o instanceof Integer){
int i=(Integer)o;
System.out.println("thr result is "+i);
}
}
}
public static class woker extends UntypedActor{
@Override
public void onReceive(Object o) throws Throwable {
if(o instanceof Integer){
int re=(Integer)o;
getSender().tell(re*re,getSelf());
}
}
}
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("watch", ConfigFactory.load("akka.config"));
ActorRef woker=system.actorOf(Props.create(future.woker.class),"woker");
ActorRef print=system.actorOf(Props.create(future.print.class),"print");
Future<Object> future = Patterns.ask(woker, 5, 1000);
Patterns.pipe(future,system.dispatcher()).to(print);
}
}