第107讲:Akka中的Future使用代码实战详解学习笔记
2015-09-25 23:26
696 查看
第107讲:Akka中的Future使用代码实战详解学习笔记
本讲分享akka中消息发送的第二种方式:send and receive
给actor发送消息后会等待目标actor的回复
用future等待目标actor的回复
actorA ask actorB,用future来接收actorB的内容。
接收到future后可以提取出future的内容。
import akka.util.Timeout;
public class ActorWithFuture {
public static class Worker extends UntypedActor {
private final LoggingAdapter log = Logging.getLogger(getContext().system(),this);
@Override
public void onReceive(Object message) throws Exception { //onReceive方法中接收其他actor发送来的消息。
if (message instanceof String){
Thread.sleep(3000); //worker收到message后睡眠3秒种后再反应
System.out.println(Thread.currentThread().getName() + "is going to work!!!");
System.out.println("The content of the received message is : " + message);
this.getSender().tell("Power comes from LOVE !",this.getSelf());
//源码:def getSender(): ActorRef = sender
//final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)
System.out.println("The sender's path = " + this.getSender().path());
getContext().stop(this.getSelf()); //停止当前actor
log.info("|||{} has stoped", this.getSelf().path());
/**源码:def getLogger(system: ActorSystem, logSource: AnyRef): LoggingAdapter = {
* val (str, clazz) = LogSource.fromAnyRef(logSource, system)
* new BusLogging(system.eventStream, str, clazz)
* }
*/
}
}
}
public static void main(String args[]) throws Exception {
System.out.println("The name of current thread is : " + thread.currentThread().getName());
ActorSystem system = ActorSystem.create("System"); //创建ActorSystem的容器。
ActorRef worker = system.actorOf(new Props(Worder.class),"WorkerActor"); //创建Worker Actor
Timeout tineout = new Timeout(Duration.create(5, "seconds")); //等待超时时间5s,
Future<Object> future = Patterns.ask(worker, "For free, for everyone, forever, for lovel", timeout); //通过ask的方式发信息给worker,等待worker的回复
//Patterns.ask方法接收消息
String result = (String) Await.result(future, timeout.duration()); //通过Await.result的方法等待future的结果
System.out.println(result);
}
}
akka的路径有akka://System/temp akka://System/user
以上内容是从王家林老师DT大数据课程第107讲的学习笔记。
DT大数据微信公众账号:DT_Spark
王家林老师QQ:1740415547
王家林老师微信号:18610086859
DT大数据梦工厂1至110集scala的所有视频、PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group
第107讲视频网站地址:
本讲分享akka中消息发送的第二种方式:send and receive
给actor发送消息后会等待目标actor的回复
用future等待目标actor的回复
actorA ask actorB,用future来接收actorB的内容。
接收到future后可以提取出future的内容。
import akka.util.Timeout;
public class ActorWithFuture {
public static class Worker extends UntypedActor {
private final LoggingAdapter log = Logging.getLogger(getContext().system(),this);
@Override
public void onReceive(Object message) throws Exception { //onReceive方法中接收其他actor发送来的消息。
if (message instanceof String){
Thread.sleep(3000); //worker收到message后睡眠3秒种后再反应
System.out.println(Thread.currentThread().getName() + "is going to work!!!");
System.out.println("The content of the received message is : " + message);
this.getSender().tell("Power comes from LOVE !",this.getSelf());
//源码:def getSender(): ActorRef = sender
//final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)
System.out.println("The sender's path = " + this.getSender().path());
getContext().stop(this.getSelf()); //停止当前actor
log.info("|||{} has stoped", this.getSelf().path());
/**源码:def getLogger(system: ActorSystem, logSource: AnyRef): LoggingAdapter = {
* val (str, clazz) = LogSource.fromAnyRef(logSource, system)
* new BusLogging(system.eventStream, str, clazz)
* }
*/
}
}
}
public static void main(String args[]) throws Exception {
System.out.println("The name of current thread is : " + thread.currentThread().getName());
ActorSystem system = ActorSystem.create("System"); //创建ActorSystem的容器。
ActorRef worker = system.actorOf(new Props(Worder.class),"WorkerActor"); //创建Worker Actor
Timeout tineout = new Timeout(Duration.create(5, "seconds")); //等待超时时间5s,
Future<Object> future = Patterns.ask(worker, "For free, for everyone, forever, for lovel", timeout); //通过ask的方式发信息给worker,等待worker的回复
//Patterns.ask方法接收消息
String result = (String) Await.result(future, timeout.duration()); //通过Await.result的方法等待future的结果
System.out.println(result);
}
}
akka的路径有akka://System/temp akka://System/user
以上内容是从王家林老师DT大数据课程第107讲的学习笔记。
DT大数据微信公众账号:DT_Spark
王家林老师QQ:1740415547
王家林老师微信号:18610086859
DT大数据梦工厂1至110集scala的所有视频、PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group
第107讲视频网站地址:
酷6网 | http://v.ku6.com/show/RjSIVgbN0MN1hmVvi3tTAQ...html |
51CTO | http://edu.51cto.com/lesson/id-75984.html |
相关文章推荐
- Struts2 用通配符方式动态调用方法
- 【Java】值得注意的一些语法点2
- C和指针 (学习之旅)
- 欢聚时代笔试题,滴滴出行编程题
- 欢聚时代笔试题,滴滴出行编程题
- 代码风格的问题
- Scala深入浅出进阶经典 第52讲:Scala中路径依赖代码实战详解
- PHP+Sphinx+Mysql做搜索引擎
- NetBeans中一个窗口的相对于另外一个窗口的位置
- Scala深入浅出进阶经典 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用
- Scala深入浅出进阶经典 第49讲:Scala中Variance代码实战及其在Spark中的应用源码解析
- C语言的关键字
- Scala深入浅出进阶经典 第48讲:Scala类型约束代码实战及其在Spark中的应用源码解析
- Python | The Zen of Python
- java研发面经:滴滴、美团、网易、58赶集等
- 《C++primer(第五版)》学习之路-第十章:泛型算法
- NetBeans中两个窗体之间如何传值
- JAVA学习第四天
- Java 权限修饰符
- 【C语言】求1+2!+3!+...+20!的和