Akka java 从不关闭 ActorRef
Akka java never close over a ActorRef
我不明白关于在回调中关闭 actor ref 的说法。
目前我正在使用
public void onReceive(Object message) throws Exception {
ActorRef senderActorRef = getSender(); //never close over a future
if (message instanceof String) {
Future<String> f =akka.dispatch.Futures.future(new Callable<String>() {
public String call() {
String value= jedisWrapper.getString("name");
senderActorRef.tell((String) message,ActorRef.noSender());
return "what";
}
}, ex);
f.onSuccess(new OnSuccessExtension(), ex);
}
}
private final class OnSuccessExtension extends OnSuccess {
@Override
public void onSuccess(Object arg0) throws Throwable {
log.info("what");
}
}
这是正确的使用方法吗?
如何在 OnSuccess 方法中传递 Sender Actor ref?
还有什么 onSuccess 和 OnComplete 之间的区别?
如果我想使用 onComplete 应该怎么用?
答案:在构造函数中传递 Sender Actor Ref。另一个用户给出的答案。
OnSuccess 是 OnComplete 的一种特殊形式。
来自 Akka 文档的 OnComplete 使用
final ExecutionContext ec = system.dispatcher();
future.onComplete(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) {
if (failure != null) {
//We got a failure, handle it here
} else {
// We got a result, do something with it
}
}
}, ec);
在构造函数中传递:
public void onReceive(Object message) throws Exception {
final ActorRef senderActorRef = getSender(); //never close over a future
if (message instanceof String) {
Future<String> f = // ...
f.onSuccess(new OnSuccessExtension(senderActorRef), ex);
}
}
private final class OnSuccessExtension extends OnSuccess {
private final ActorRef senderActorRef;
public OnSuccessExtension(ActorRef senderActorRef) {
this.senderActorRef = senderActorRef;
}
@Override
public void onSuccess(Object arg0) throws Throwable {
log.info("what");
// use senderActorRef
}
}
我不明白关于在回调中关闭 actor ref 的说法。 目前我正在使用
public void onReceive(Object message) throws Exception {
ActorRef senderActorRef = getSender(); //never close over a future
if (message instanceof String) {
Future<String> f =akka.dispatch.Futures.future(new Callable<String>() {
public String call() {
String value= jedisWrapper.getString("name");
senderActorRef.tell((String) message,ActorRef.noSender());
return "what";
}
}, ex);
f.onSuccess(new OnSuccessExtension(), ex);
}
}
private final class OnSuccessExtension extends OnSuccess {
@Override
public void onSuccess(Object arg0) throws Throwable {
log.info("what");
}
}
这是正确的使用方法吗? 如何在 OnSuccess 方法中传递 Sender Actor ref? 还有什么 onSuccess 和 OnComplete 之间的区别? 如果我想使用 onComplete 应该怎么用?
答案:在构造函数中传递 Sender Actor Ref。另一个用户给出的答案。 OnSuccess 是 OnComplete 的一种特殊形式。 来自 Akka 文档的 OnComplete 使用
final ExecutionContext ec = system.dispatcher();
future.onComplete(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) {
if (failure != null) {
//We got a failure, handle it here
} else {
// We got a result, do something with it
}
}
}, ec);
在构造函数中传递:
public void onReceive(Object message) throws Exception {
final ActorRef senderActorRef = getSender(); //never close over a future
if (message instanceof String) {
Future<String> f = // ...
f.onSuccess(new OnSuccessExtension(senderActorRef), ex);
}
}
private final class OnSuccessExtension extends OnSuccess {
private final ActorRef senderActorRef;
public OnSuccessExtension(ActorRef senderActorRef) {
this.senderActorRef = senderActorRef;
}
@Override
public void onSuccess(Object arg0) throws Throwable {
log.info("what");
// use senderActorRef
}
}