akka-guice


License

License

Categories

Categories

GUI User Interface Guice Application Layer Libs Dependency Injection Akka Container Microservices Reactive libraries
GroupId

GroupId

com.sandinh
ArtifactId

ArtifactId

akka-guice_2.11
Last Version

Last Version

3.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

akka-guice
akka-guice
Project URL

Project URL

https://github.com/ohze/akka-guice
Project Organization

Project Organization

com.sandinh
Source Code Management

Source Code Management

https://github.com/ohze/akka-guice

Download akka-guice_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.sandinh/akka-guice_2.11/ -->
<dependency>
    <groupId>com.sandinh</groupId>
    <artifactId>akka-guice_2.11</artifactId>
    <version>3.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.sandinh/akka-guice_2.11/
implementation 'com.sandinh:akka-guice_2.11:3.2.0'
// https://jarcasting.com/artifacts/com.sandinh/akka-guice_2.11/
implementation ("com.sandinh:akka-guice_2.11:3.2.0")
'com.sandinh:akka-guice_2.11:jar:3.2.0'
<dependency org="com.sandinh" name="akka-guice_2.11" rev="3.2.0">
  <artifact name="akka-guice_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.sandinh', module='akka-guice_2.11', version='3.2.0')
)
libraryDependencies += "com.sandinh" % "akka-guice_2.11" % "3.2.0"
[com.sandinh/akka-guice_2.11 "3.2.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.11
com.google.inject.extensions : guice-assistedinject jar 4.1.0
com.typesafe.akka : akka-actor_2.11 jar 2.5.4

test (2)

Group / Artifact Type Version
com.typesafe.akka : akka-testkit_2.11 jar 2.5.4
org.scalatest : scalatest_2.11 jar 3.0.4

Project Modules

There are no modules declared in this project.

akka-guice

Build Status

What?

This is a very simple (so very stable) scala library for injecting Akka using Guice.

Why?

Google search akka guice => some articles:

=> So I create this library :D

How?

  1. install akka-guice from maven center ex, add to build.sbt: libraryDependencies += "com.sandinh" %% "akka-guice" % "3.2.0"

  2. use akka-guice

see the test files & source code for detail. It's very simple!

  • ChildActor: The foo parameter will be injected
class ChildActor @Inject() (@Named("fooName") foo: String) extends Actor ...
  • AssistedChildActor: Sometimes an Actor gets some of its constructor parameters from the Guice Injector and others from the caller.

    Parameter foo of AssistedChildActor's constructor is taken from Guice. arg1 & arg2 from the caller.

class AssistedChildActor(foo: String, arg1: Int, arg2: String) extends Actor ...

object AssistedChildActor {
  class Factory @Inject() (@Named("fooName") foo: String) extends ActorFactory[AssistedChildActor] {
    def create(args: Any*): AssistedChildActor = args match {
      case Seq(arg1: Int, arg2: String) => new AssistedChildActor(foo, arg1, arg2)
      case _                            => throw new IllegalArgumentException
    }
  }
}
  • ParentActor: extends com.sandinh.akuice.ActorInject & use injectActor method to inject child actors. The injectActor method need an implicit akka.actor.ActorRefFactory to create actor (using ActorRefFactory#actorOf method).

    ParentActor extends Actor => it has an implicit val context: ActorContext (ActorContext extends ActorRefFactory).

class ParentActor @Inject() (val injector: Injector) extends Actor with ActorInject {
  private val child1 = injectActor[ChildActor]
  private val child2 = injectActor[ChildActor]("child2")
  private val assistedChild = injectActor[AssistedChildActor, AssistedChildActor.Factory](1, "arg2 value")
  ...
}
  • Service.scala: This is not an Actor but because it extends com.sandinh.akuice.ActorInject => we can use injectTopActor method.

    Note that, we can also use injectActor method. The injected actor will be a child of the implicit ActorRefFactory in the scope.

@Singleton
class Service @Inject() (val injector: Injector) extends ActorInject {
  private val parentRef = injectTopActor[ParentActor]

  def hello(sender: ActorRef) = parentRef.tell("hello!", sender)
}
  • AkkaModule:
class AkkaModule(system: ActorSystem) extends AbstractModule {
  def configure(): Unit = {
    bind(classOf[ActorSystem]).toInstance(system)
    //other binds
  }
}
  • AkuiceSpec:
"Akuice" must {
    "inject actors: receive replied messages when call Service.hello" in {
      val injector = Guice.createInjector(new AkkaModule(system))
      val service = injector.getInstance(classOf[Service])
      service.hello(self)

      //expectMsg ...
    }
  }

Changelogs

see CHANGES.md

Licence

This software is licensed under the Apache 2 license: http://www.apache.org/licenses/LICENSE-2.0

Copyright 2014-2017 Sân Đình (http://sandinh.com)

Versions

Version
3.2.0
3.1.3
3.1.2
3.1.1
3.1.0
3.0.0
2.0.1
2.0.0
1.2.0
1.1.0
1.0.0