trait PekkoGuiceSupport extends AnyRef
Support for binding actors with Guice.
Mix this trait in with a Guice AbstractModule to get convenient support for binding actors. For example:
class MyModule extends AbstractModule with PekkoGuiceSupport { def configure = { bindActor[MyActor]("myActor") bindTypedActor(HelloActor(), "hello-actor") } }
Then to use the above actor in your application, add a qualified injected dependency, like so:
class MyController @Inject() ( @Named("myActor") myActor: ActorRef, helloActor: ActorRef[HelloActor.SayHello], val controllerComponents: ControllerComponents, ) extends BaseController { ... }
- Self Type
- PekkoGuiceSupport with AbstractModule
- Source
- PekkoGuiceSupport.scala
- Alphabetic
- By Inheritance
- PekkoGuiceSupport
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def bindActor[T <: Actor](name: String, props: (Props) => Props = identity)(implicit arg0: ClassTag[T]): Unit
Bind an actor.
Bind an actor.
This will cause the actor to be instantiated by Guice, allowing it to be dependency injected itself. It will bind the returned ActorRef for the actor will be bound, qualified with the passed in name, so that it can be injected into other components.
- T
The class that implements the actor.
- name
The name of the actor.
- props
A function to provide props for the actor. The props passed in will just describe how to create the actor, this function can be used to provide additional configuration such as router and dispatcher configuration.
- def bindActorFactory[ActorClass <: Actor, FactoryClass](implicit arg0: ClassTag[ActorClass], arg1: ClassTag[FactoryClass]): Unit
Bind an actor factory.
Bind an actor factory.
This is useful for when you want to have child actors injected, and want to pass parameters into them, as well as have Guice provide some of the parameters. It is intended to be used with Guice's AssistedInject feature.
Let's say you have an actor that looks like this:
class MyChildActor @Inject() (db: Database, @Assisted id: String) extends Actor { ... }
So
db
should be injected, whileid
should be passed. Now, define a trait that takes the id, and returns the actor:trait MyChildActorFactory { def apply(id: String): Actor }
Now you can use this method to bind the child actor in your module:
class MyModule extends AbstractModule with PekkoGuiceSupport { def configure = { bindActorFactory[MyChildActor, MyChildActorFactory] } }
Now, when you want an actor to instantiate this as a child actor, inject
MyChildActorFactory
:class MyActor @Inject() (myChildActorFactory: MyChildActorFactory) extends Actor with InjectedActorSupport { def receive { case CreateChildActor(id) => val child: ActorRef = injectedChild(myChildActorFactory(id), id) sender() ! child } }
- ActorClass
The class that implements the actor that the factory creates
- FactoryClass
The class of the actor factory
- final def bindTypedActor[T](actorModule: Aux[T], name: String)(implicit arg0: ClassTag[T]): Unit
Bind a typed actor.
Bind a typed actor.
Binds
Behavior[T]
andActorRef[T]
for the given message typeT
to the given ActorModule and actor name, so that it can be injected into other components. Use this variant ofbindTypedActor
when using the "functional programming" style of defining your actor's behavior and it needs to be injected with dependencies in dependency scope (such as Configuration).The binding of the Behavior happens by installing the given
ActorModule
into this GuiceModule
. Make sure to add the Provides annotation on theBehavior
-returning method to bind (by convention this is theapply
method).Note that, while the name is used when spawning the actor in the
ActorSystem
, it is NOT used as a name qualifier for the binding. This is so that you don't need to use Named to qualify all injections of typed actors. Use the underlying API to create multiple, name-annotated bindings.- T
The type of the messages the typed actor can handle.
- actorModule
The
ActorModule
that provides the behavior of the typed actor.- name
The name of the typed actor.
- Annotations
- @ApiMayChange()
- final def bindTypedActor[T](behavior: Behavior[T], name: String)(implicit arg0: ClassTag[T]): Unit
Bind a typed actor.
Bind a typed actor.
Binds
Behavior[T]
andActorRef[T]
for the given message typeT
to the given Behavior value and actor name, so that it can be injected into other components. Use this variant ofbindTypedActor
when using the "functional programming" style of defining your actor's behavior and it doesn't depend on anything in dependency scope.Note that, while the name is used when spawning the actor in the
ActorSystem
, it is NOT used as a name qualifier for the binding. This is so that you don't need to use Named to qualify all injections of typed actors. Use the underlying API to create multiple, name-annotated bindings.- T
The type of the messages the typed actor can handle.
- behavior
The
Behavior
of the typed actor.- name
The name of the typed actor.
- Annotations
- @ApiMayChange()
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @HotSpotIntrinsicCandidate() @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @HotSpotIntrinsicCandidate() @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @HotSpotIntrinsicCandidate() @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @HotSpotIntrinsicCandidate() @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @HotSpotIntrinsicCandidate() @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated
(Since version 9)