Guice with Scala Actors

160 views
Skip to first unread message

Thomas Suckow

unread,
Jan 30, 2012, 10:51:48 PM1/30/12
to google-guice
I am using Guice with Scala. I am looking into adding the first actors
into my application and am a little turned off of using annotations to
inject as that would create a bunch more classes. Can anyone think of
an easier way to wire them in?

I suppose it really may not be any more classes as the annotation
would take the place of a common base class. If anyone has experience
with using Actors I would love to hear your story.

Regards,
Thomas

Alen Vrečko

unread,
Feb 15, 2012, 9:42:52 AM2/15/12
to google-guice
Sorry for such late reply. Exam period. Didn't check ML. Will reply as
somebody might stumble upon this.

I did GSoC work for Scala/Akka (will do thesis from it in the coming
weeks).

I only use Akka I imagine it works the same for Scala Actors. I'd
recommend using Akka - more of everything. ;)

In Akka you can do (Scala Actors similar code):

class MyActor @Inject (foo:Foo, bar:Bar) extends Actor {
@Inject baz:Baz = null

def receive = {
....
}
}

You have ctor, field and method injection just like in Java.

You then need to manually call the injector

val actorRef = actorOf(injector.getInstance(classOf[MyActor])).start()

There is nothing special about the actor instance itself. What is
special is how they communicate with each other.

You can also do AOP in your Actor as Guice is creating the instance.
Just keep in mind intercepting receive will actually intercept the
"partial function" and not the actual "receive".

Cheers
Alen

Thomas Suckow

unread,
Feb 15, 2012, 11:52:39 AM2/15/12
to google...@googlegroups.com
I did eventually switch to akka actors.

I also made a helper provider to do what you recommended.

https://github.com/Deathbobomega/Weave/blob/8e16fcd0005fb78ba66a7f803166465340d63ebb/core-api/src/main/scala/net/codingwell/weave/ActorProvider.scala

https://github.com/Deathbobomega/Weave/blob/8e16fcd0005fb78ba66a7f803166465340d63ebb/core/src/main/scala/net/codingwell/weave/LocalExecutorModule.scala


Thanks!
Thomas Suckow

> --
> You received this message because you are subscribed to the Google Groups "google-guice" group.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to google-guice...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
>

Alen Vrečko

unread,
Feb 16, 2012, 10:06:52 AM2/16/12
to google-guice
Glad to hear everything works.

Just one more thing: You might want to think about using Akka 2.0 API.
RC1 is out and the final is scheduled to come out at the end of the
month. There are some substantial changes to the API so you might want
to have that in mind.

http://akka.io/docs/akka/snapshot/project/migration-guide-1.3.x-2.0.x.html

Cheers
Alen

On Feb 15, 5:52 pm, Thomas Suckow <tsuc...@gmail.com> wrote:
> I did eventually switch to akka actors.
>
> I also made a helper provider to do what you recommended.
>
> https://github.com/Deathbobomega/Weave/blob/8e16fcd0005fb78ba66a7f803...
>
> https://github.com/Deathbobomega/Weave/blob/8e16fcd0005fb78ba66a7f803...
Reply all
Reply to author
Forward
0 new messages