运维开发网
广告位招商联系QQ:123077622
 
广告位招商联系QQ:123077622

斯卡拉 – 这对于自我内部的akka​​演员类

运维开发网 https://www.qedev.com 2020-07-31 09:47 出处:网络 作者:运维开发网整理
假设我有一个非常简单的actor类,它接收任何消息并打印到控制台. class SimpleActor extends Actor{ def receive: Receive = { case message => println(s"[${this}][${self}] received message: ${message}") } }
假设我有一个非常简单的actor类,它接收任何消息并打印到控制台.

class SimpleActor extends Actor{

    def receive: Receive = {
      case message =>
        println(s"[${this}][${self}] received message: ${message}")
    }
  }

  val simpleActor = actorSystem.actorOf(Props[SimpleActor], "simpleActor")
  simpleActor ! "Hey"

正如你所看到的,我在这里使用了这个和自我,两者都有不同的价值观.它的输出类似于:

[[email protected]][Actor[akka://ActorDemo/user/simpleActor#934141660]] received message: Hey

我想了解self和this之间的区别,因为在复杂场景(生产系统)中,如果actor中断,例如:抛出一个异常而不是我认为这个值会发生变化.

这是对扩展Actor特性的对象的经典java引用,而self是对ActorRef的引用,这是你需要发送消息的内容(!或者告诉和?或者询问)

>您无法向此发送消息

>你不应该传递对这个外部actor的引用,而传递对self的引用是完全正常的,实际上当你从另一个actor向一个actor发送一条消息时它会被隐式发送.如果将其传递给另一个对象,则会冒成actor的状态封装的风险.请记住,与演员沟通的唯一方式是通过消息,即使用其ActorRef

>在演员重启后,self将保持有效,也就是说你可以继续向同一个ActorRef(self)发送消息.仅当actor停止时,对ActorRef的引用不再有效,发送到该地址的消息将以Dead Letters结尾.

>在actor重启后,这将不再有效.实例化Actor类型的新对象以清除可能由于失败而被破坏的actor状态.

What restarting means

Unless the failure is specifically recognizable, the third cause cannot be ruled out, which leads to the conclusion that the internal state needs to be cleared out. If the supervisor decides that its other children or itself is not affected by the corruption—e.g. because of conscious application of the error kernel pattern—it is therefore best to restart the child. This is carried out by creating a new instance of the underlying Actor class and replacing the failed instance with the fresh one inside the child’s ActorRef; the ability to do this is one of the reasons for encapsulating actors within special references. The new actor then resumes processing its mailbox, meaning that the restart is not visible outside of the actor itself with the notable exception that the message during which the failure occurred is not re-processed.

Actor Reference and Path Equality

Note that a restart of an actor caused by a failure still means that it is the same actor incarnation, i.e. a restart is not visible for the consumer of the ActorRef.

What is the Difference Between Actor Reference and Path?

An actor reference designates a single actor and the life-cycle of the reference matches that actor’s life-cycle; an actor path represents a name which may or may not be inhabited by an actor and the path itself does not have a life-cycle, it never becomes invalid. You can create an actor path without creating an actor, but you cannot create an actor reference without creating corresponding actor.

You can create an actor, terminate it, and then create a new actor with the same actor path. The newly created actor is a new incarnation of the actor. It is not the same actor. An actor reference to the old incarnation is not valid for the new incarnation. Messages sent to the old actor reference will not be delivered to the new incarnation even though they have the same path.

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消