我有以下问题。。。
我正在使用Java中的Akka 2.3.6,并希望完成以下操作:
Future<Object> future = ask(actor, new GetPOIDataMessage(tenant), Timeout.durationToTimeout(duration));
future.onSuccess(new OnComplete<NonSimpleObject>() {
public void onComplete(Throwable failure, final NonSimpleObject data) {
if (failure != null) {
deferredResult.setErrorResult("An error occured during the request");
} else {
deferredResult.setResult(data);
}
}
}, ec);
NonSimpleObject是从参与者发回的消息的类型。我在编译代码时遇到以下错误:
error: method onSuccess in interface Future<T> cannot be applied to given types;
[error] future.onSuccess(new OnComplete<NonSimpleObject>() {
[error] ^
[error] required: PartialFunction<Object,U>,ExecutionContext
[error] found: <anonymous OnComplete< NonSimpleObject >>,ExecutionContext
[error] reason: cannot infer type-variable(s) U
[error] (argument mismatch; <anonymous OnComplete< NonSimpleObject >> cannot be converted to PartialFunction<Object,U>)
[error] where U,T are type-variables:
[error] U extends Object declared in method <U>onSuccess(PartialFunction<T,U>,ExecutionContext)
[error] T extends Object declared in interface Future`
我无法解码。我现在好像有点不知所措。因此,它可以很好地处理字符串。我在网上找不到其他使用不同字符串的例子。
感谢您为我们提供正确的指导。一月
我认为您的问题源于尝试使用OnComplete<NonSimpleObject>
而不是OnComplete<Object>
。您拥有的Future
是Future<Object>
,因此您只能使用OnComplete<Object>
是理所当然的。我不认为你可以投任何一个,因为这似乎对我不起作用
public class NonSimpleObject{
public final int i;
public final String s;
public NonSimpleObject(String s, int i){
this.s = s;
this.i = i;
}
}
public class SimpleActor extends UntypedActor{
public SimpleActor(){
}
public void onReceive(Object msg){
getSender().tell(new NonSimpleObject("foo", 11), getContext().self());
}
}
import scala.concurrent.Future;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import static akka.pattern.Patterns.ask;
import akka.dispatch.*;
class AskTest{
public static void main(String[] args) {
ActorSystem system = ActorSystem.create();
ActorRef ref = system.actorOf(Props.create(SimpleActor.class));
Future<Object> fut = ask(ref, "foo", 1000);
fut.onComplete(new OnComplete<Object>(){
public void onComplete(Throwable t, Object result){
System.out.println(((NonSimpleObject)result).s);
}
}, system.dispatcher());
}
}
Java和Scala与Futures之间的互操作性似乎并不那么好。这个例子在纯scala中要容易得多,在Java中看起来有点笨拙。在Scala中,Future
有mapTo
,所以您可以为Future
获得正确的类型,但我没有看到任何可以在Java中使用的模拟。
编辑
玩了一段时间后,我发现了一种在Future
上使用mapTo
来获得正确打字的方法。你可以尝试这样的东西,但正如我所说,它如何为mapTo
:获得所需的Scala ClassTag
,这很麻烦
import scala.concurrent.Future;
import scala.reflect.ClassTag$;
import scala.reflect.ClassTag;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import static akka.pattern.Patterns.ask;
import akka.dispatch.*;
class AskTest{
public static void main(String[] args) {
ActorSystem system = ActorSystem.create();
ActorRef ref = system.actorOf(Props.create(SimpleActor.class));
ClassTag<NonSimpleObject> tag = ClassTag$.MODULE$.apply(NonSimpleObject.class);
Future<NonSimpleObject> fut = ask(ref, "foo", 1000).mapTo(tag);
fut.onComplete(new OnComplete<NonSimpleObject>(){
public void onComplete(Throwable t, NonSimpleObject result){
System.out.println(result.s);
}
}, system.dispatcher());
}
}