Akka and cake pattern

前端 未结 2 1287
攒了一身酷
攒了一身酷 2021-02-07 23:53

I\'m confused how to ensure that my Actors have the appropriate dependencies using the cake pattern. I\'m still getting to grips with this, and I can\'t find any examples anywhe

2条回答
  •  一生所求
    2021-02-08 00:25

    One additional gotcha, as pointed out by Owen, is that creating actors using actorOf(Props[MyClass]) won't work for inner classes. Ie: the following will fail:

    trait MyComponent {
      class MyActor {
        def receive = ...
      }
    }
    
    new MyComponent {
      val myActorRef = system.actorOf( Props[MyActor] )
    }
    

    According to the documentation at http://doc.akka.io/docs/akka/snapshot/scala/actors.html,

    if they are not declared within a top-level object then the enclosing instance’s this reference needs to be passed as the first argument

    However, this does not seem to be supported by the scaladoc Props method signatures. The only way around this I found was to use the Props(actor: Actor) constructor, instantiating the actor mysql and passing it to Props.

    val myActorRef = system.actorOf( Props( new MyActor(arg1, arg2) ) )
    

    Would be interested to know if there's a better way..

提交回复
热议问题