Hi Markus, since extensions have get-or-create semantics they cannot take arguments: how would you know which argument was provided to the instance you actually get back? Therefore extensions are only providers, not consumers (well, they can reference other extensions if you carefully avoid loops).
What this means is that you could just move the creation of that actor into the extension. OTOH every actor with a name is already a singleton which can be looked up, so I don’t see a need for using an extension to achieve this effect. Regards, Roland 16 jan 2014 kl. 07:39 skrev Markus Jais <[email protected]>: > One more question. How I can pass an ActorRef to my extension? > > I want to do something like this: > > class MyExtension (val myActorRef: ActorRef) extends Extension { > //.... > } > object MyExtension extends ExtensionId[MyExtension] > with ExtensionIdProvider { > override def lookup = MyExtension > override def createExtension(system: ExtendedActorSystem) = new > MyExtension(????????????????) > override def get(system: ActorSystem): MyExtension = super.get(system.) > } > > trait ActorDataCollector { self: Actor => > def myActorRef = { > MyExtension(context.system).myActorRef > } > } > > I want to pass an ActorRef to MyExension that I create in the main method to > my extension and then use a trait to make it easier to use it. Of course the > extension gets created with createExtension which seems to be called by Akka > itself. > > I thought I can do something like this in main > val myActor = system.actorOf(Props[MyActor], "my") > val myExtension = new MyExtension(myActor) > > > And then in my REST actors (I use Spray 1.2): > > class EchoActor extends Actor with ActorLogging with ActorServerCheck { > ... > import MyActor._ > .... > myActorRef ! EchoSuccess > > > This should guarantee that there is only once instance of that actor and > would make it easy to use. > > But it doesn't work as the Extension get's initialized on it's own as it > seems. I cannot set the Actor where the "?????????????" are. > > Is this a good idea to pass an Actor to an extension and what is the > recommend way to do it? > > > Cheers, > > Markus > > > > > > > > > > > > > > > > > > > > > > Markus Jais <[email protected]> schrieb am 14:02 Montag, 13.Januar 2014: > Thanks Alec! I will do it like that. > > Markus > > > Alec Zorab <[email protected]> schrieb am 13:44 Montag, 13.Januar 2014: > That's certainly how I tend to do it. We use the same thing for injecting > configs, dbs etc. Almost anything that you want to be a "singleton" but you > need to be able to swap in different implementations for testing, etc. > > > On 13 January 2014 12:39, Markus Jais <[email protected]> wrote: > Hi Alec, > > thanks. That looks great. > > Would it make sense to hide the extension in a trait and use it as a mixin? > Meaning, the trait could have the code like: > > counter = CountExtension(context.system) > > and in my Actor I only have some like: > > class MyActor with Counting { > counter.increment() > } > > Markus > > > > > > > > > Alec Zorab <[email protected]> schrieb am 11:21 Montag, 13.Januar 2014: > Use an akka extension: > http://doc.akka.io/docs/akka/2.2.3/scala/extending-akka.html > > > > On 13 January 2014 10:15, Markus Jais <[email protected]> wrote: > Hello, > > I need to integrate a Java library that is used to collects lot's of runtime > information about errors, failures, etc. > It has many data collectors. There may be better ways to do this but I need > to use this library for current projects. > > The collectors should exist once per JVM (we are not using Akka cluster right > now). If the JVM crashes, the data can be lost. > Everything is in memory. > > How would I use such a singleton in the best way within an Akka/Spray service? > > I had the following ideas: > > 1) Just use a global singleton and something like a getInstance method which > can be called form everywhere incl. each actor > > 2) Hide the singleton within an actor as a private member. Create only one > such Actor and pass it around. Send messages to that actor and translate the > message to calls to the data collector. This is ugly as I have to pass around > the actor. > > 3) Use a trait similar ActorLogging which I can add to each Actor that needs > those collectors and guarantee within the trait that there is only once > instance. > > > > Are there any other options? Who has had to deal with a similar problem? Any > recomendations for how to do this the Akka way? > > The old Java applications just use Spring and Singleton scop but the > Scala/Akka services don't use Spring. > > > Cheers, > > Markus > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. > > > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ > >>>>>>>>>> Check the FAQ: http://akka.io/faq/ > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/groups/opt_out. Dr. Roland Kuhn Akka Tech Lead Typesafe – Reactive apps on the JVM. twitter: @rolandkuhn -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: http://akka.io/faq/ >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/groups/opt_out.
