我有一个包含方法的生成的Java接口:
public Future<?> getCustomersAsync(AsyncHandler<Customer> asyncHandler);
我想使用Akka实现它。我写了以下内容:
override def getCustomerAsync(asyncHandler: AsyncHandler[Customer]): Future[_] = {
myActorRef.ask(GetCustomer, system.actorOf(Props[Responder]))
}
问题是ask
返回scala.concurrent.Future[Any]
并且方法必须返回java.util.concurrent.Future[?]
:
Error:(33, 17) type mismatch;
found : scala.concurrent.Future[Any]
required: java.util.concurrent.Future[?]
myActorRef.ask(GetCustomer, system.actorOf(Props[Responder]))
^
我该如何转换?
那么将它们转换是不切实际的,因为scalaFuture
不提供中断功能或任何其他取消机制。因此,没有直接的完全证明方法可以通过中断或通过Future中的方法调用来取消未来。
因此,最简单的解决方案是:如果不需要取消,将是:
def convert[T](x:Future[T]):java.util.concurrent.Future[T]={
new concurrent.Future[T] {
override def isCancelled: Boolean = throw new UnsupportedOperationException
override def get(): T = Await.result(x, Duration.Inf)
override def get(timeout: Long, unit: TimeUnit): T = Await.result(x, Duration.create(timeout, unit))
override def cancel(mayInterruptIfRunning: Boolean): Boolean = throw new UnsupportedOperationException
override def isDone: Boolean = x.isCompleted
}
}
但是如果您仍然需要cancel
,则将显示一个残障修复程序
在这里。但是我不推荐它
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句