Always use the internal same thread execution context impl on Scala 2.12

This commit is contained in:
Johan Andrén 2020-03-12 17:07:34 +01:00
parent 7d790ef328
commit 779993e7c3

View file

@ -4,12 +4,10 @@
package akka.dispatch.internal package akka.dispatch.internal
import akka.actor.ReflectiveDynamicAccess
import akka.annotation.InternalApi import akka.annotation.InternalApi
import akka.dispatch.BatchingExecutor import akka.dispatch.BatchingExecutor
import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext
import scala.util.control.NonFatal
/** /**
* Factory to create same thread ec. Not intended to be called from any other site than to create [[akka.dispatch.ExecutionContexts#parasitic]] * Factory to create same thread ec. Not intended to be called from any other site than to create [[akka.dispatch.ExecutionContexts#parasitic]]
@ -18,20 +16,14 @@ import scala.util.control.NonFatal
*/ */
@InternalApi @InternalApi
private[dispatch] object SameThreadExecutionContext { private[dispatch] object SameThreadExecutionContext {
def apply(): ExecutionContext = {
try { private val sameThread = new ExecutionContext with BatchingExecutor {
// we don't want to introduce a dependency on the actor system to use the same thread execution context override protected def unbatchedExecute(runnable: Runnable): Unit = runnable.run()
val dynamicAccess = new ReflectiveDynamicAccess(getClass.getClassLoader) override protected def resubmitOnBlock: Boolean = false // No point since we execute on same thread
dynamicAccess.getObjectFor[ExecutionContext]("scala.concurrent.Future$InternalCallbackExecutor$").get override def reportFailure(t: Throwable): Unit =
} catch { throw new IllegalStateException("exception in sameThreadExecutionContext", t)
case NonFatal(_) =>
// fallback to custom impl in case reflection is not available/possible
new ExecutionContext with BatchingExecutor {
override protected def unbatchedExecute(runnable: Runnable): Unit = runnable.run()
override protected def resubmitOnBlock: Boolean = false // No point since we execute on same thread
override def reportFailure(t: Throwable): Unit =
throw new IllegalStateException("exception in sameThreadExecutionContext", t)
}
}
} }
def apply(): ExecutionContext = sameThread
} }