如何存储 scala actor 引用?
您好,我是 Scala 新手,我无法弄清楚如何在第二个参与者中存储参与者引用,以便稍后发送消息。在我的代码中,我尝试向一个演员发送一条测试消息。当它收到此消息时,它应该存储对第二个参与者的引用(OutputChannel),并且稍后应该能够向第二个参与者发送消息。我不想使用reply(),因为我需要仅在调用响应时发送消息。这是代码。感谢您的帮助!
import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.mutable.ArrayBuffer
import scala.actors.OutputChannel
object testactors {
case object TestMessage
case object Respond
class TestActor(name: String) extends Actor {
private var source : ArrayBuffer[OutputChannel[Any]] = new ArrayBuffer
def act() {
loop {
react{
case TestMessage =>
println("i received a TestMessage " + name)
source += sender
case Respond =>
println("i received a ResponseMessage " + name)
}
}
}
def sendMessage(dest: Actor) = dest ! TestMessage
def respond = {
println("responding... " + name)
source(0) ! Respond
}
}
def main(args: Array[String]) {
val actor1 = new TestActor("one")
actor1.start
val actor2 = new TestActor("two")
actor2.start
actor1.sendMessage(actor2)
Thread.sleep(5000)
actor2.respond
}
}
Hello I am new to Scala and I have failed to figure out how one can store an actor reference within a second actor, for sending a message at a later time. In my code I try to send a test message to one actor. when it receives this message it should store the reference (OutputChannel) to the second actor and at a later time should be able to send a message to the second actor. I did not want to use the reply() as I need the message to be sent only when i invoke the respond. Here is the code. Thanks for any help!
import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.mutable.ArrayBuffer
import scala.actors.OutputChannel
object testactors {
case object TestMessage
case object Respond
class TestActor(name: String) extends Actor {
private var source : ArrayBuffer[OutputChannel[Any]] = new ArrayBuffer
def act() {
loop {
react{
case TestMessage =>
println("i received a TestMessage " + name)
source += sender
case Respond =>
println("i received a ResponseMessage " + name)
}
}
}
def sendMessage(dest: Actor) = dest ! TestMessage
def respond = {
println("responding... " + name)
source(0) ! Respond
}
}
def main(args: Array[String]) {
val actor1 = new TestActor("one")
actor1.start
val actor2 = new TestActor("two")
actor2.start
actor1.sendMessage(actor2)
Thread.sleep(5000)
actor2.respond
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1.您可以创建一个集中的参与者注册表。为了避免重新发明轮子,您可以使用一个好的现有实现 - Akka 的 Actor Registry(或者至少从中获得灵感)。
2 您可以避免使用通过反应循环传递的演员引用的可变列表:
1. You can create a centralized actor registry. To avoid reinventing the wheel, you can use a good existing implementation - Akka's Actor Registry (or, at least, get inspired with it).
2 You can avoid using mutable list of actor references passing it through react loop:
一种方法是创建一个 Actor Factory 来存储可以从任何地方获取的 Actor。
消息可以是对象(没有“有效负载”),也可以是包含数据的类
以下是一些参与者类型。 Actor2 实例是“即时”实例化的,并存储在 ActorFactory 中供以后使用,就像在 main 中显式声明的 Actor1 实例一样
。以下 ActorFactory 创建并存储 Actor。在这里,您可以创建一种类型的参与者的多个实例,并按名称存储。
这个的输出是
One way to do it is to create an Actor Factory that stores actors that you can grab from anywhere.
Messages can be either objects (with no "payload"), or can be classes that contain data
Here are a couple of actor types. The Actor2 instance is instantiated "on-the-fly", and stored in the ActorFactory for later use, as is the Actor1 instance that is explicitly declared in the main
The following ActorFactory creates and stores actors. Here, you can create multiple instances of a type of actor, and store by name.
The output of this is