知识库--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.
- 知识库--Sending and Receiving Messages By Akka Using Java(139)
- 5 Sending and Receiving Messages
- 知识库--Creating Actors By Akka Using Java(139)
- Sending and Receiving Packets
- 知识库--Using Akka Refs and Transactions(124)
- Receiving and Dispatching Messages
- android wearable-Transferring Assets,Sending and Receiving Messages,Handling Data Layer Events
- 知识库--Creating Transaction in Java Using Akka(124)
- Sending and receiving broadcast message in Android
- 知识库--Akka Commit and Rollback Events in Java(136)
- Sending SMS WAP Push messages using the ActiveXperts SMS and MMS Toolkit.
- RUDP之二 —— Sending and Receiving Packets
- xmlhttp 收发二进制数据 (Sending and Receiving Binary Data)
- 知识库--Akka 事务配置-using jvm(134)
- 发布和发送消息(Posting and Sending Messages)
- 知识库--The Write Skew Anomaly Fixed By Akka(136)
- Posting Messages和Sending Messages
- Silent Receiving of SMS messages
- zookeeper集群介绍
- 欧拉函数的算法实现
- 练习7
- WIN 7 系统 问题记录
- Jquery滑动到某一位置代码
- 知识库--Sending and Receiving Messages By Akka Using Java(139)
- B1003
- 最简洁完整的Java高级教程之一(数据结构、集合、网络编程、多线程)
- 用C++实现日期类
- 练习8
- 练习9
- 自学安卓之路:为什么选择学安卓开发?
- Android图片预览功能(可旋转、缩放、左右切换)
- 练习10