Akka Actor Inbox_信箱

Akka Actor Inbox_信箱


Inbox_send_receive

Inbox 形象的表示为Actor的信箱,具有收发信息的功能,所以,Inbox 对象有这两种方法:

  • send(target, "hello") ;

  • receive(Duration. create(1, TimeUnit. SECONDS) )

以下Inbox 使用示例,首先定义一个Actor,

package com.usoft3;

import akka.actor.UntypedActor;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class MyActor extends UntypedActor {

    private int x;
    private int y;

    public MyActor(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        System.out.println("message=" + message);
        int result = x + y;
        this.getSender().tell(result, this.getSelf());
        this.getContext().stop(this.getSelf());
    }
}


下面是用Inbox 和上边的 Actor 进行交互,如下,

package com.usoft3;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import akka.actor.Props;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import java.util.concurrent.TimeUnit;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class InboxDemo {
    public static void main(String args[]) {
        ActorSystem system = ActorSystem.create("myActorSystem");
        Inbox inbox = Inbox.create(system);
        ActorRef cal = system.actorOf(Props.create(MyActor.class, 5, 7), "myActor");
        inbox.send(cal, "hello world!");
        Integer result = (Integer) inbox.receive((FiniteDuration) Duration.create(1, TimeUnit.SECONDS));
        System.out.println("result=" + result);
    }
}


Inbox_watch

使用Inbox 监视一个Actor的关闭

如下定义一个Actor,

package com.usoft3;

import akka.actor.UntypedActor;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class MyActor extends UntypedActor {

    private int x;
    private int y;

    public MyActor(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void onReceive(Object message) throws Exception {
        System.out.println("message=" + message);
        int result = x + y;
        this.getSender().tell(result, this.getSelf());
        this.getContext().stop(this.getSelf());
    }
}


使用Inbox 监视该Actor,

package com.usoft3;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import akka.actor.Props;
import akka.actor.Terminated;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import java.util.concurrent.TimeUnit;

/**
 * Created by liyanxin on 2015/1/12.
 */
public class InboxWatchDemo {

    public static void main(String args[]) {
        ActorSystem system = ActorSystem.create("myActorSystem");
        Inbox inbox = Inbox.create(system);
        ActorRef cal = system.actorOf(Props.create(MyActor.class, 5, 7), "myActor");
        inbox.watch(cal);
        cal.tell("good afternoon", ActorRef.noSender()); //这个消息没有发送者
        Terminated terminated = (Terminated) inbox.receive((FiniteDuration) Duration.create(1, TimeUnit.SECONDS));
        System.out.println(terminated.toString());
        System.out.println(terminated.getActor().path());//actor path = akka://myActorSystem/user/myActor
    }
}

运行结果如下,

message=good afternoon

[INFO] [01/12/2015 15:57:12.442] [myActorSystem-akka.actor.default-dispatcher-3] [akka://myActorSystem/deadLetters] Message [java.lang.Integer] from Actor[akka://myActorSystem/user/myActor#-2054229435] to Actor[akka://myActorSystem/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

Terminated(Actor[akka://myActorSystem/user/myActor#-2054229435])

akka://myActorSystem/user/myActor

===================END===================

转载于:https://my.oschina.net/xinxingegeya/blog/366567

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值