akka实战项目5--future模式

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值