[英]What Akka Java classes to implement Future callbacks with?
Akka Java here. Akka Java在这里。 My classes:
我的课程:
class SomeActor extends UntypedActor {
// ...
}
class SomeMessage {
// ...
}
// Inside onReceive method for another actor:
Future<Fizz> fizzFut = Patterns.ask(someActor, someMsg, 500)
fizzFut.onComplete(new FizzHandler())
class FizzHandler extends akka.dispatch.OnComplete<Fizz> {
@Override
void onComplete(Throwable error, Fizz result) {
if(error != null) {
// Handle error.
} else {
// Handle success.
}
// TODO: Now how do I send a message back "inside" the
// actor system?
}
}
At runtime I get the following exception: 在运行时,我得到以下异常:
[ERROR] [08/23/2015 05:55:09.490] [myapp-akka.actor.default-dispatcher-4]
[akka://myapp/user/AnotherActor] No signature of method:
scala.concurrent.impl.Promise$DefaultPromise.onComplete() is applicable for argument
types: (com.me.myapp.FizzHandler) values: [<function1>]
Possible solutions: onComplete(scala.Function1, scala.concurrent.ExecutionContext),
isCompleted(), complete(scala.util.Try), tryComplete(scala.util.Try)
So I tried implementing a scala.Function
subclass but apparently that class is final
and cannot be extended. 所以我尝试实现了一个
scala.Function
子类,但显然该类是final
,无法扩展。
So two concerns here: 这里有两个问题:
onComplete
handler. onComplete
处理程序使用的Java API类。 Any ideas?; FizzHandler#onComplete(...)
method is running, how do I send a message back "inside" my actor system? FizzHandler#onComplete(...)
方法正在运行,我如何在我的演员系统“内部”发回消息? The Inbox
? Inbox
? Using Akka 2.3.8 and scala 2.10 : 使用Akka 2.3.8和scala 2.10 :
I use the separate onFailure
and onSuccess
methods which take an akka.dispatch.OnFailure
and akka.dispatch.OnSuccess<T>
respectively. 我使用单独的
onFailure
和onSuccess
方法,分别采用akka.dispatch.OnFailure
和akka.dispatch.OnSuccess<T>
。 For example: 例如:
future.onFailure(new OnFailure() {
@Override
public void onFailure(Throwable failure) throws Throwable {
// handle failure
}
}, context().dispatcher());
and 和
future.onSuccess(new OnSuccess<Object>() {
@Override
public void onSuccess(Object result) throws Throwable {
// handle successful value return
}
}, context().dispatcher());
(if you're calling this outside an actor you'll have to change where you get the dispatcher) (如果你在演员之外打电话给你,你必须改变调度员的位置)
In your example using onComplete you're missing the dispatcher. 在使用onComplete的示例中,您缺少调度程序。 onComplete takes 2 parameters - the
handler
function and the dispatcher
which should be used to run the call-back on once it's ready. onComplete接受2个参数 -
handler
函数和dispatcher
handler
,它应该用于在准备就绪后运行回调。 So the fixed sample is: 所以固定样本是:
import akka.actor.ActorSystem;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;
import scala.concurrent.Future;
import scala.runtime.BoxedUnit;
final ActorSystem sys = ActorSystem.create();
final Future<String> successful = Futures.successful("");
successful.onComplete(new OnComplete<String>() {
@Override
public void onComplete(Throwable failure, String success) throws Throwable {
}
}, sys.dispatcher());
Note that once Scala 2.12 hits it's stable release and Akka compiles using it (soon) it will emit FunctionN
classes in a compatible way with Java lambda expressions, then you'll be able to write onComplete(d -> {}, dispatcher)
. 请注意,一旦Scala 2.12命中它的稳定版本并且Akka使用它(很快)编译它将以兼容的方式使用Java lambda表达式发出
FunctionN
类,然后您将能够编写onComplete(d -> {}, dispatcher)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.