转载:https://blog.csdn.net/liubenlong007/article/details/54575312
和java线程中的future挺像的,可以将一个actor的返回结果重定向到另一个actor中进行处理,主actor或者进程无需等待actor的返回结果。
package akka.future;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.pattern.Patterns;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;
/**
* Created by liubenlong on 2017/1/16.
*/
public class AskMain {
public static void main(String[] args) throws Exception {
ActorSystem system = ActorSystem.create("strategy", ConfigFactory.load("akka.config"));
ActorRef printActor = system.actorOf(Props.create(PrintActor.class), "PrintActor");
ActorRef workerActor = system.actorOf(Props.create(WorkerActor.class), "WorkerActor");
//等等future返回
Future<Object> future = Patterns.ask(workerActor, 5, 1000);
int result = (int) Await.result(future, Duration.create(3, TimeUnit.SECONDS));
System.out.println("result:" + result);
//不等待返回值,直接重定向到其他actor,有返回值来的时候将会重定向到printActor
Future<Object> future1 = Patterns.ask(workerActor, 8, 1000);
Patterns.pipe(future1, system.dispatcher()).to(printActor);
workerActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
package akka.future;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
/**
* Created by liubenlong on 2017/1/12.
*/
public class PrintActor extends UntypedActor {
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
@Override
public void onReceive(Object o) throws Throwable {
log.info("akka.future.PrintActor.onReceive:" + o);
if (o instanceof Integer) {
log.info("print:" + o);
} else {
unhandled(o);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
package akka.future;
import akka.actor.*;
import akka.event.Logging;
import akka.event.LoggingAdapter;
/**
* Created by liubenlong on 2017/1/12.
*/
public class WorkerActor extends UntypedActor {
private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
@Override
public void onReceive(Object o) throws Throwable {
log.info("akka.future.WorkerActor.onReceive:" + o);
if (o instanceof Integer) {
Thread.sleep(1000);
int i = Integer.parseInt(o.toString());
getSender().tell(i*i, getSelf());
} else {
unhandled(o);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
输出结果:
[INFO] [01/16/2017 16:22:32.983] [strategy-akka.actor.default-dispatcher-5] [akka://strategy/user/WorkerActor] akka.future.WorkerActor.onReceive:5
result:25
[INFO] [01/16/2017 16:22:33.984] [strategy-akka.actor.default-dispatcher-5] [akka://strategy/user/WorkerActor] akka.future.WorkerActor.onReceive:8
[INFO] [01/16/2017 16:22:34.997] [strategy-akka.actor.default-dispatcher-6] [akka://strategy/user/PrintActor] akka.future.PrintActor.onReceive:64
[INFO] [01/16/2017 16:22:34.997] [strategy-akka.actor.default-dispatcher-6] [akka://strategy/user/PrintActor] print:64
- 1
- 2
- 3
- 4
- 5
其实这里和之前的文章中那样,在workerActor执行完毕以后,向printActor发消息不也行吗?
参考资料
- 书籍《java高并发程序设计》
- AKKA官方文档