Akka Java 在这里。我的课程:
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?
}
}
在运行时,我得到以下异常:
[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)
所以我尝试实现一个scala.Function
子类,但显然该类是final
并且无法扩展。
这里有两个问题:
onComplete
处理程序使用的Java API类。有任何想法吗?;和FizzHandler#onComplete(...)
方法正在运行,我该如何发送消息&#34;在&#34;内部&#34;我的演员系统? Inbox
?答案 0 :(得分:2)
使用Akka 2.3.8 和scala 2.10 :
我使用分别带有java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mainuser.math.passgrade4.passgrade4/com.mainuser.math.passgrade4.passgrade4.level1}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
...
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
at com.mainuser.math.passgrade4.passgrade4.level1.onCreate(level1.java:34)
...
和onFailure
的单独的 onSuccess
和akka.dispatch.OnFailure
方法。例如:
akka.dispatch.OnSuccess<T>
和
future.onFailure(new OnFailure() {
@Override
public void onFailure(Throwable failure) throws Throwable {
// handle failure
}
}, context().dispatcher());
(如果您在演员外面称呼这个,那么您必须改变调度员的位置)
答案 1 :(得分:2)
在使用onComplete的示例中,您错过了调度程序。 onComplete需要2个参数 - handler
函数和dispatcher
一旦准备好就应该用来运行回调。所以固定样本是:
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());
请注意,一旦Scala 2.12达到它的稳定版本并且Akka使用它(很快)编译,它将以与Java lambda表达式兼容的方式发出FunctionN
个类,然后你就可以了写onComplete(d -> {}, dispatcher)
。