Thanks for sharing. I will take a closer look next week.
/Patrik

On Thu, Jan 16, 2014 at 2:15 PM, Chanan Braunstein <[email protected]>wrote:

> Hi Patrik, Roland & anyone else that wants to take a look,
>
> I created a Play module (with a sample project) register actor
> automatically with Guice: https://github.com/chanan/AkkaGuice
>
> Would you mind taking a look and giving feedback?
>
> Thanks,
> Chanan.
>
>
> On Saturday, January 11, 2014 4:22:47 PM UTC-5, Chanan Braunstein wrote:
>>
>> Hi Patrik,
>>
>> There are a few reasons why I don't want the actors defined and
>> instantiated inside the controller. For example, for testing purposes, I
>> would like to pass in to the controller a probe so we can write good unit
>> tests.
>>
>> Thanks for the link to the Spring-Java Activator. I think something like:
>> https://github.com/typesafehub/activator-akka-
>> java-spring/blob/master/src/main/java/sample/SpringExtension.java for
>> Guice is what I am looking for. Does anyone have one already? If not, I
>> will attempt to write one.
>>
>> Chanan.
>>
>> On Saturday, January 11, 2014 9:48:41 AM UTC-5, Patrik Nordwall wrote:
>>>
>>> Hi Chanan,
>>>
>>>
>>> On Fri, Jan 10, 2014 at 7:24 PM, Chanan Braunstein <[email protected]>wrote:
>>>
>>>> Hi Roland,
>>>>
>>>> Thanks for your reponse. I do think what I am trying to do is correct.
>>>> The purpose of what I am asking for is not to inject ActorRef's into
>>>> Actors. It is to inject ActorRef's into Play Controllers.
>>>>
>>>
>>> Why is it important to inject the the ActorRefs into Play Controllers?
>>> Why not create the actors there (using IndirectActorProducer) or in an Akka
>>> extension <http://doc.akka.io/docs/akka/2.2.3/java/extending-akka.html>?
>>>
>>> Have you looked at the Typesafe Activator Akka Spring 
>>> template<http://typesafe.com/activator/template/akka-java-spring>
>>> ?
>>>
>>> Regards,
>>> Patrik
>>>
>>>
>>>>
>>>> We already do so by Manually Creating the provider functions in the
>>>> GuiceModule. However this problematic since we cannot
>>>> use IndirectActorProducer if we do that.
>>>>
>>>> So here is the code example:
>>>>
>>>> public class GuiceModule extends AbstractModule {
>>>>
>>>> @Override
>>>>  protected void configure() {
>>>>  //bind code goes here
>>>>
>>>>  }
>>>>
>>>>  @Provides @Named("NewAppActor")
>>>>  ActorRef getNewAppActor() {
>>>>  return Akka.system().actorOf(NewAppActor.Props(), "NewAppActor");
>>>> //This should be using IndirectActorProducer but it cannot since the
>>>> injector does not exist yet
>>>>  }
>>>> }
>>>>
>>>> And a Play controller:
>>>>
>>>> public class Application extends Controller {
>>>>
>>>> private final ActorRef newAppActor;
>>>>
>>>> @Inject
>>>>  public Application(@Named("NewAppActor") ActorRef newAppActor) {
>>>>  this.newAppActor = newAppActor;
>>>>  }
>>>> ...
>>>> }
>>>>
>>>> So to sum up, there is one actual problem and one nice to have
>>>> (although solving the nice to have would actually solve the problem as
>>>> well):
>>>>
>>>> 1. Problem: Inside GuiceModule, I do not have access to Injector, so we
>>>> cannot use IndirectActorProducer to create the ActorRef, so we cannot
>>>> use injection in that Actor.
>>>>
>>>> 2. Nice to have: It would be nice if we can scan our code, find all the
>>>> actors and register them in GuiceModule. This can use IndirectActorProducer
>>>> if the scanning happens after the injector is created.
>>>>
>>>> I already have code that scans the code for certain actors that are
>>>> marked as needing scheduling and starts them via the scheduler, so I was
>>>> wondering if someone already solved this same issue I am facing. There was
>>>> that mention on stackoverflow as I said, but I couldn't find the code they
>>>> mentioned.
>>>>
>>>> On Friday, January 10, 2014 4:46:37 AM UTC-5, rkuhn wrote:
>>>>
>>>>> Hi Chanan,
>>>>>
>>>>> I’ll leave more specific points to Patrik, but I’d like to remark on
>>>>> the general strategy. Injecting resources into Actors for their use is a
>>>>> very reasonable thing to do, which is why IndirectActorProducer exists.
>>>>> Creating actors, however, is something which inherently must always be 
>>>>> done
>>>>> by another actor in order to properly define the supervision hierarchy.
>>>>> Guice cannot really do that, because an ActorSystem is its own 
>>>>> encapsulated
>>>>> container and apart from the unfortunate exception of `system.actorOf()`
>>>>> Guice cannot drive the creation—it can only be involved by the parent 
>>>>> actor
>>>>> as you are doing it. As a side note, I dream of a world where
>>>>> system.actorOf() is deprecated and eventually removed, simply because it 
>>>>> is
>>>>> such a gross and problematic hack, both internally and semantically.
>>>>>
>>>>> Akka provides several ways to wire together an actor system: you can
>>>>> create child actors, pass their reference to other child actors via their
>>>>> constructor, you can introduce actors to each other by sending references
>>>>> in messages, and finally there is also `context.actorSelection` (if all
>>>>> else fails). Therefore I do not see the need to use a DI framework for
>>>>> injecting ActorRefs into actors, I believe we have covered that base
>>>>> already. If you have a use case which is not supported by these tools,
>>>>> please let us know!
>>>>>
>>>>> Regards,
>>>>>
>>>>> Roland
>>>>>
>>>>> 10 jan 2014 kl. 01:53 skrev Chanan Braunstein <[email protected]>:
>>>>>
>>>>> Hello,
>>>>>
>>>>> We have been using @Names & @Provides from Guice to register actors to
>>>>> be injected in Play controllers like so:
>>>>>
>>>>> @Provides @Named("NewAppActor")
>>>>> ActorRef getNewAppActor() {
>>>>> *Return* Akka.system().actorOf(NewAppActor.Props(), "NewAppActor");
>>>>> }
>>>>>
>>>>> Where .Props() is a static method that returns a Props object.
>>>>>
>>>>> This works fine but 2 problems:
>>>>>
>>>>> 1. As our system grows we have a need to inject services into the
>>>>> actors as well. We found a way to do so by implementing
>>>>> IndirectActorProducer like so:
>>>>>
>>>>> public class GuiceInjectedActor implements IndirectActorProducer {
>>>>>  final Injector injector;
>>>>>  final Class<? extends Actor> actorClass;
>>>>>
>>>>> public GuiceInjectedActor(Injector injector, Class<? extends Actor>
>>>>> actorClass) {
>>>>>  this.injector = injector;
>>>>>  this.actorClass = actorClass;
>>>>>  }
>>>>>
>>>>>  @Override
>>>>>  public Class<? extends Actor> actorClass() {
>>>>>  return actorClass;
>>>>>  }
>>>>>
>>>>>  @Override
>>>>>  public Actor produce() {
>>>>>  return injector.getInstance(actorClass);
>>>>>  }
>>>>> }
>>>>>
>>>>> This injects any services we have defined and works well. The problem
>>>>> is that we can use this class inside our @Provides method since that
>>>>> @Provides method is defined in the GuiceModule, so the injector does not
>>>>> exist yet.
>>>>>
>>>>> 2) As the system grows we have more and more classes defined in
>>>>> GuiceModule. We would like a way to automatically register our actors
>>>>> within Guice. We found a mention of this in a stackoverflow answer here:
>>>>> http://stackoverflow.com/a/13947079 - but when we look in Github, we
>>>>> don't see the classes mentioned and we aren't sure how to use those from
>>>>> Java anyway.
>>>>>
>>>>> So, does anyone know how to either: register actors in Guice that have
>>>>> services themselves and/or automatically registers actors with the @Names
>>>>> attribute?
>>>>>
>>>>> Thanks,
>>>>> Chanan.
>>>>>
>>>>>
>>>>> --
>>>>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>> >>>>>>>>>> Search the archives: https://groups.google.com/grou
>>>>> p/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 <http://typesafe.com/> – Reactive apps on the JVM.
>>>>> twitter: @rolandkuhn
>>>>>  <http://twitter.com/#!/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.
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> Patrik Nordwall
>>> Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
>>> Twitter: @patriknw
>>>
>>>   --
> >>>>>>>>>> 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.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw

-- 
>>>>>>>>>>      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.

Reply via email to