Scala 如何让一个演员在另一个演员中不受阻碍?
目前在一个现有的scala-akka http项目中,我们有一群参与者需要告诉其他参与者消息 在参与者中,我们将通过以下方式获得参与者的引用:Scala 如何让一个演员在另一个演员中不受阻碍?,scala,akka,akka-http,Scala,Akka,Akka Http,目前在一个现有的scala-akka http项目中,我们有一群参与者需要告诉其他参与者消息 在参与者中,我们将通过以下方式获得参与者的引用: class OurActor extends Actor { private implicit val timeout: Timeout = 15.seconds private val otherActor: ActorRef = Await.result[ActorRef](context.system.actorSelectio
class OurActor extends Actor {
private implicit val timeout: Timeout = 15.seconds
private val otherActor: ActorRef = Await.result[ActorRef](context.system.actorSelection("path/to/other/actor").resolveOne()(timeout), 15.seconds)
override def receive: Receive = {
case SomeMessage => {
...
dataSets.foreach(_.foreach(otherActor ! _))
}
}
}
我对
Await.result
的用法有点困惑,因为它在当前线程中阻塞。我可以使用什么替代方法?您不一定需要actorRef,但您也可以直接使用选择器向一个参与者(甚至多个参与者)发送消息:
class MetronomeJobAlertActor extends Actor {
private implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
private implicit val ec = context.dispatcher
private implicit val sys = context.system
private val otherActorSelection: ActorSelection = context.system.actorSelection("path/to/other/actor")
override def receive: Receive = {
case SomeMessage => {
...
dataSets.foreach(otherActorSelection ! _))
}
}
您不一定需要actorRef,但也可以直接使用选择器向参与者(甚至多个参与者)发送消息:
class MetronomeJobAlertActor extends Actor {
private implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
private implicit val ec = context.dispatcher
private implicit val sys = context.system
private val otherActorSelection: ActorSelection = context.system.actorSelection("path/to/other/actor")
override def receive: Receive = {
case SomeMessage => {
...
dataSets.foreach(otherActorSelection ! _))
}
}
您可以将另一个参与者作为
OurActor
object OurActor {
def props(otherActorRef: ActorRef) = Props(classOf[OurActor],otherActorRef)
}
class OurActor(otherActor: ActorRef) extends Actor {
private implicit val timeout: Timeout = 15.seconds
override def receive: Receive = {
case SomeMessage => {
...
otherActor ! _
}
}
}
当您实例化您的参与者时:
val otherActor = actorSystem.actorOf(OtherActor.props, "otherActor")
val ourActor = actorSystem.actorOf(OurActor.props(otherActor) "ourActor")
您可以将另一个参与者作为
OurActor
object OurActor {
def props(otherActorRef: ActorRef) = Props(classOf[OurActor],otherActorRef)
}
class OurActor(otherActor: ActorRef) extends Actor {
private implicit val timeout: Timeout = 15.seconds
override def receive: Receive = {
case SomeMessage => {
...
otherActor ! _
}
}
}
当您实例化您的参与者时:
val otherActor = actorSystem.actorOf(OtherActor.props, "otherActor")
val ourActor = actorSystem.actorOf(OurActor.props(otherActor) "ourActor")
如果您想确保它是可解析的,也许您可以直接在
未来的中执行数据集.foreach
。大概是这样的:
class OurActor extends Actor {
override def receive: Receive = {
case SomeMessage => {
...
context.system.actorSelection("path/to/other/actor").resolveOne().map { actorRef =>
dataSets.foreach(_.foreach(actorRef ! _))
}
}
}
}
这意味着dataSets.foreach
语句将异步执行。如果数据集
是可变的,则需要小心,因为事情将在不同的线程中运行。另外,未来
可能会失败,因此您可能还需要考虑该场景。如果您想确保它是可解决的,也许您可以直接在未来
中执行数据集。foreach
。大概是这样的:
class OurActor extends Actor {
override def receive: Receive = {
case SomeMessage => {
...
context.system.actorSelection("path/to/other/actor").resolveOne().map { actorRef =>
dataSets.foreach(_.foreach(actorRef ! _))
}
}
}
}
这意味着dataSets.foreach
语句将异步执行。如果数据集
是可变的,则需要小心,因为事情将在不同的线程中运行。另外,未来的
可能会失败,因此您可能还需要考虑该场景。当您实例化所有参与者时,您可以将另一个参与者的ActorRef传递/注入到我们的参与者
-类我们的参与者(aRef:ActorRef)扩展参与者
@ccheneson,这是一个有效的替代方案。你能提供一个答案吗?当你实例化你所有的参与者时,你可以将另一个参与者的ActorRef传递/注入到OurActor
-类OurActor(aRef:ActorRef)扩展了actor
@ccheneson,这是一个有效的替代方案。你能提供一个答案吗?