Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection with Akka

I use Guice in my application quite a lot. Recently i start to learn akka actors and felt like refactoring my application with it.

However upfront i am already wondering how all my guice will work with actors. I went on searching on google and it is kinda a bit messy.

The most up to date docs that i have found on the subject are theses:

http://letitcrash.com/post/55958814293/akka-dependency-injection

http://eng.42go.com/tag/guice/

which do not advocate same thing.

I must confess i still need to read a lot, i am at the beginning of learning akka. I Did few examples and red few things, but i don't want to go to deep into something to realize later that i will have many problems.

So my question is as of today, what is the consensus on how to use Akka Actors with dependency injection.

What kind of injection is possible ? Can we wire actors with object/other actors/....

Can anyway please outline in a concise way something that can help me to understand what is possible and what is the best practices ?

like image 243
MaatDeamon Avatar asked Jan 02 '14 02:01

MaatDeamon


Video Answer


2 Answers

I know you are working in Akka with Guice and Scala, but Typesafe provides a tutorial describing how things work in Akka with Spring and Java. This can provide a good starting point for understanding how dependency injection fits into the Actor lifecycle for your situation.

Meanwhile, here is some sample code from their documentation for using a factory method to inject constructor arguments:

class DependencyInjector(applicationContext: AnyRef, beanName: String) extends IndirectActorProducer {
  override def actorClass = classOf[Actor]
  override def produce = // obtain fresh Actor instance from DI framework ...
}
val actorRef = system.actorOf(Props(classOf[DependencyInjector], applicationContext, "hello"), "helloBean")

Here are some guidelines compiled by Typesafe on the matter.

Finally, note the following from the documentation:

"When using a dependency injection framework, actor beans MUST NOT have singleton scope."

like image 122
Vidya Avatar answered Oct 20 '22 01:10

Vidya


The latest activator has a tutorial for Akka with Guice.

like image 2
Cristian Vrabie Avatar answered Oct 20 '22 00:10

Cristian Vrabie