Vincent
Vincent

Reputation: 1136

Mix custom trait with ActorSystem

I would like to mixin a custom trait with my Akka ActorSystem to modify the behavior of its existing methods. However, since the ActorSystem is created by invoking the apply method on the ActorSystem companion object, and the actual constructor is package private, I cannot simply mixin a trait with the constructor. So I decided to write a new factory within the package akka.actor (based on the code of the original companion object, see Github):

package akka.actor

import akka.actor.setup.ActorSystemSetup

import akka.util.Reflect
import com.typesafe.config.{ Config, ConfigFactory }

object CustomActorSystem{
    def apply(){
        val setup = ActorSystemSetup(BootstrapSetup(None, None, None))
        val bootstrapSettings = setup.get[BootstrapSetup]
        val cl = bootstrapSettings.flatMap(_.classLoader).getOrElse(Reflect.findClassLoader())
        val appConfig = bootstrapSettings.flatMap(_.config).getOrElse(ConfigFactory.load(cl))
        val defaultEC = bootstrapSettings.flatMap(_.defaultExecutionContext)

        new ActorSystemImpl("default", appConfig, cl, defaultEC, None, setup).start() // here I could mixin my custom traits
    }
}

Now something strange happens: the compiler complains about the constructor invocation:

too many arguments for constructor ActorSystemImpl: (name: String, applicationConfig: com.typesafe.config.Config, classLoader: ClassLoader, defaultExecutionContext: Option[scala.concurrent.ExecutionContext], guardianProps: Option[akka.actor.Props])akka.actor.ActorSystemImpl
[error]         new ActorSystemImpl("default", appConfig, cl, defaultEC, None, setup).start()

However, based on the code (see Github), I am using the right type signature. Can someone explain this?

And by the way, if someone knows a more clever way to change the behavior of the native methods of my ActorSystem, I am also interested.

Upvotes: 1

Views: 159

Answers (1)

johanandren
johanandren

Reputation: 11479

The ActorSystemImpl is an internal API and I would strongly discourage trying to extend or interact directly with it, binary and source compatibility is not maintained for the internal APIs so ignoring this advice could lead to your app breaking in patch releases of Akka or just misbehaving in generally surprising ways.

A better way would be to define the alternative methods in a custom Akka Extension and make sure to use those throughout your code base instead of the corresponding built in methods.

Upvotes: 0

Related Questions