阿卡演员问及类型安全 [英] Akka Actor ask and Type Safety

查看:301
本文介绍了阿卡演员问及类型安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

致电时要求上的阿卡演员,一个未来[任意] 返回,我所要做的通过显式转换 future.mapTo [MyType的]

When calling ? or ask on an Akka Actor, a Future[Any] is returned and I have to do an explicit cast via future.mapTo[MyType].

我不喜欢失去这种类型的安全性。如果我使用期货直接(不带演员),我可以明确地返回未来[的MyType] 并保持类型安全。

I don't like losing this type safety. If I use Futures directly (with no actors) I can explicitly return Future[MyType] and maintain type safety.

我的具体使用情况涉及一个演员委派它的消息,两名儿童演员,然后从这些行动者聚集的结果,并返回到父的发件人。我父母的接收方法看起来类似于阿卡文档这种方式:

HTTP://doc.akka。 IO /文档/阿卡/ 2.0 /斯卡拉/ futures.html#For_Com prehensions

val f1 = actor1 ? msg
val f2 = actor2 ? msg

val f3 = for {
  a ← f1.mapTo[Int]
  b ← f2.mapTo[Int]
  c ← ask(actor3, (a + b)).mapTo[Int]
} yield c

有没有更好的方式来实现我的使用情况?

Is there a better way to achieve my use case?

推荐答案

尝试类型化的演员的。基本上,他们允许你使用强类型的特性/接口而不是通过交换消息的演员互动。幕后阿卡实现这些接口与动态代理并执行异步神奇。

Try typed actors. Basically they allow you to interact with an actor using strongly typed traits/interfaces rather than by exchanging messages. Behind the scenes Akka implements these interfaces with a dynamic proxy and does the asynchronous magic.

类型化的演员可以返回曾与不同的,强类型的返回值的方法(从文档上面提到的):

Typed actor can return have methods with different, strongly typed return values (from documentation mentioned above):

def squareDontCare(i: Int): Unit //fire-forget

def square(i: Int): Future[Int] //non-blocking send-request-reply

def squareNowPlease(i: Int): Option[Int] //blocking send-request-reply

def squareNow(i: Int): Int //blocking send-request-reply

这些方法重新presents 告诉,而其余的都是不同的口味要求

These method represents tell while the remaining ones are different flavours of ask.

这篇关于阿卡演员问及类型安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆