Identifier and factory for an extension. Is used to look up an extension from the ActorSystem, and possibly create
an instance if no instance was already registered. The extension can also be listed in the actor system configuration
to have it eagerly loaded and registered on actor system startup.
*Scala API*
The ExtensionId for an extension written in Scala is best done by letting it be the companion object of the
extension. If the extension will be used from Java special care needs to be taken to provide a get method with the
concrete extension type as return (as this will not be inferred correctly by the Java compiler with the default
implementation)
Example:
object MyExt extends ExtensionId[Ext] {
overridedef createExtension(system: ActorSystem[_]): MyExt = new MyExt(system)
// Java API: retrieve the extension instance for the given system.def get(system: ActorSystem[_]): MyExt = apply(system)
}
class MyExt(system: ActorSystem[_]) extends Extension {
...
}
// can be loaded eagerly on system startup through configuration// note that the name is the JVM/Java class name, with a dollar sign in the end// and not the Scala object name
akka.actor.typed.extensions = ["com.example.MyExt$"]
// Allows access like this from Scala
MyExt().someMethodOnTheExtension()
// and from Java
MyExt.get(system).someMethodOnTheExtension()
*Java API*
To implement an extension in Java you should first create an ExtensionId singleton by implementing a static method
called getInstance, this is needed to be able to list the extension among the akka.actor.typed.extensions in the configuration
and have it loaded when the actor system starts up.
public class MyExt extends AbstractExtensionId<MyExtImpl> {
// single instance of the identifierprivatefinal static MyExt instance = new MyExt();
// protect against other instances than the singletonprivate MyExt() {}
// This static method singleton accessor is needed to be able to enable the extension through config when// implementing extensions in Java.
public static MyExt getInstance() {
return instance;
}
public MyExtImpl createExtension(ActorSystem<?> system) {
returnnew MyExtImpl();
}
// convenience accessor
public static MyExtImpl get(ActorSystem<?> system) {
return instance.apply(system);
}
}
public class MyExtImpl implements Extension {
...
}
// can be loaded eagerly on system startup through configuration
akka.actor.typed.extensions = ["com.example.MyExt"]
// Allows access like this from Scala
MyExt.someMethodOnTheExtension()
// and from Java
MyExt.get(system).someMethodOnTheExtension()
Identifier and factory for an extension. Is used to look up an extension from the
ActorSystem
, and possibly create an instance if no instance was already registered. The extension can also be listed in the actor system configuration to have it eagerly loaded and registered on actor system startup.*Scala API*
The
ExtensionId
for an extension written in Scala is best done by letting it be the companion object of the extension. If the extension will be used from Java special care needs to be taken to provide aget
method with the concrete extension type as return (as this will not be inferred correctly by the Java compiler with the default implementation)Example:
*Java API*
To implement an extension in Java you should first create an
ExtensionId
singleton by implementing a static method calledgetInstance
, this is needed to be able to list the extension among theakka.actor.typed.extensions
in the configuration and have it loaded when the actor system starts up.For testing purposes extensions typically provide a concrete ExtensionSetup that can be used in akka.actor.setup.ActorSystemSetup when starting the ActorSystem to replace the default implementation of the extension.
The concrete extension type
ExtensionSetup