@@ -111,27 +111,18 @@ private[spark] case class RpcEnvConfig(
111111 securityManager : SecurityManager )
112112
113113/**
114- * A RpcEnv implementation must have a companion object with an
115- * `apply(config: RpcEnvConfig): RpcEnv` method so that it can be created via Reflection.
116- *
117- * {{{
118- * object MyCustomRpcEnv {
119- * def apply(config: RpcEnvConfig): RpcEnv = {
120- * ...
121- * }
122- * }
123- * }}}
114+ * A RpcEnv implementation must have a [[RpcEnvFactory ]] implementation with an empty constructor
115+ * so that it can be created via Reflection.
124116 */
125117private [spark] object RpcEnv {
126118
127- private def getRpcEnvCompanion (conf : SparkConf ): AnyRef = {
119+ private def getRpcEnvFactory (conf : SparkConf ): RpcEnvFactory = {
128120 // Add more RpcEnv implementations here
129- val rpcEnvNames = Map (" akka" -> " org.apache.spark.rpc.akka.AkkaRpcEnv " )
121+ val rpcEnvNames = Map (" akka" -> " org.apache.spark.rpc.akka.AkkaRpcEnvFactory " )
130122 val rpcEnvName = conf.get(" spark.rpc" , " akka" )
131- val rpcEnvClassName = rpcEnvNames.getOrElse(rpcEnvName.toLowerCase, rpcEnvName)
132- val companion = Class .forName(
133- rpcEnvClassName + " $" , true , Utils .getContextOrSparkClassLoader).getField(" MODULE$" ).get(null )
134- companion
123+ val rpcEnvFactoryClassName = rpcEnvNames.getOrElse(rpcEnvName.toLowerCase, rpcEnvName)
124+ Class .forName(rpcEnvFactoryClassName, true , Utils .getContextOrSparkClassLoader).
125+ newInstance().asInstanceOf [RpcEnvFactory ]
135126 }
136127
137128 def create (
@@ -142,13 +133,20 @@ private[spark] object RpcEnv {
142133 securityManager : SecurityManager ): RpcEnv = {
143134 // Using Reflection to create the RpcEnv to avoid to depend on Akka directly
144135 val config = RpcEnvConfig (conf, name, host, port, securityManager)
145- val companion = getRpcEnvCompanion(conf)
146- companion.getClass.getMethod(" apply" , classOf [RpcEnvConfig ]).
147- invoke(companion, config).asInstanceOf [RpcEnv ]
136+ getRpcEnvFactory(conf).create(config)
148137 }
149138
150139}
151140
141+ /**
142+ * A factory class to create the [[RpcEnv ]]. It must have an empty constructor so that it can be
143+ * created using Reflection.
144+ */
145+ private [spark] trait RpcEnvFactory {
146+
147+ def create (config : RpcEnvConfig ): RpcEnv
148+ }
149+
152150/**
153151 * An end point for the RPC that defines what functions to trigger given a message.
154152 *
0 commit comments