知识库--Sending and Receiving Messages By Akka Using Java(139)

来源:互联网 发布:千牛怎么查买家淘宝号 编辑:程序博客网 时间:2024/05/19 13:09

1 Creating Actors
2 Sending and Receiving Messages
3 Working with Multiple Actors
4 Coordinating Actors
5 Using Typed Actors
6 Typed Actors and Murmurs
7 Mixing Actors and STM
8 Using Transactors
9 Coordinating Typed Actors
10 Remote Actors
11 Limitations of the Actor-Based Model
12 epliogue

replyUnsafe()method 回应消息,不检查发送者

import akka.actor.UntypedActor;import akka.actor.ActorRef;import akka.actor.Actors;import akka.actor.ActorTimeoutException;public class FortuneTeller extends UntypedActor {  public void onReceive(final Object name) {      getContext().replyUnsafe(String.format("%s you'll rock", name));  }    public static void main(final String[] args) {    final ActorRef fortuneTeller =       Actors.actorOf(FortuneTeller.class).start();    try {      final Object response = fortuneTeller.sendRequestReply("Joe");      System.out.println(response);    } catch(ActorTimeoutException ex) {      System.out.println("Never got a response before timeout");          } finally {      fortuneTeller.stop();          }  }}

结果

Joe you'll rock

replySafe()method 回应消息,检查发送者

import akka.actor.UntypedActor;import akka.actor.Actors;import akka.actor.ActorRef;import akka.actor.ActorTimeoutException;public class FortuneTeller extends UntypedActor {  public void onReceive(final Object name) {    if(getContext().replySafe(String.format("%s you'll rock", name)))      System.out.println("Message sent for " + name);    else      System.out.println("Sender not found for " + name);  }    public static void main(final String[] args) {    final ActorRef fortuneTeller =       Actors.actorOf(FortuneTeller.class).start();    try {      fortuneTeller.sendOneWay("Bill");      final Object response = fortuneTeller.sendRequestReply("Joe");      System.out.println(response);    } catch(ActorTimeoutException ex) {      System.out.println("Never got a response before timeout");          } finally {      fortuneTeller.stop();          }  }}

结果

Sender not found for BillMessage sent for JoeJoe you'll rock

分析
The call to sendRequestReply() blocks while waiting for a response, but the call to sendOneWay() is nonblocking and yields no response. If we want to receive a response but don’t want to wait for it, we can use the more elaborate method sendRequestReplyFuture(), which will return a Future object. We can go
about doing work until we want the response, at which time we can either block or query the future object to see whether a response is available. Similarly, on the side of the actor, we can get the senderFuture from the context reference and communicate through that right away or later when we have a response ready.

It is quite convenient that Akka passes the sender references under the covers when it sends a message. This eliminates the need to pass the sender explicitly as part of the message and removes so much noise and effort in the code.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 广州出租车丢了东西怎么办 广州的士丢了东西怎么办 网上找兼职被骗了怎么办 海信空调开不了机怎么办 海信空调遥控器开不了怎么办 学生遭套路贷反被仲裁怎么办 赏脸打错字尝脸怎么办 红掌的花变黑了怎么办 红掌花苞发黑了怎么办 水培植物腐根了怎么办 水培绿萝水发臭怎么办 水里养花根烂掉怎么办 桅子花叶子发黑怎么办 大株月季烂根怎么办 月季水浇多了烂根的怎么办 金桔盆栽烂根怎么办 盆栽的长寿果树烂根怎么办 家里的石榴烂根怎么办 山桔盆栽烂根怎么办 养的植物烂根怎么办 桅子花叶子长霉怎么办 紫薇花叶子干了怎么办 高层玻璃阳台往下看恐高怎么办 比熊放阳台叫怎么办 海员入职体检不合格怎么办 联币金融立案投资人怎么办 联币金融的投资怎么办 养老保险领了几个月就挂了怎么办 高铁餐吧乘务员东西卖不出去怎么办 铁路局如果查出有乙肝怎么办 在火车站丢了东西怎么办 在新乡火车站丢了东西怎么办 自己的行李忘到高铁安检怎么办 高铁二等座睡觉怎么办 空少岁数大了怎么办 美国留学生办欧洲签证怎么办 苏州小区不让装充电桩怎么办 饿了么运力不足怎么办 书法落款写偏了怎么办 辐射4运行不流畅怎么办 vgs币忘了映射怎么办