我有这个特质
trait NonBlockingGoodness extends DataStore {
import akka.dispatch.{ Future, ExecutionContext }
import akka.util.duration._
import akka.util.Timeout
implicit val ec = ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)
implicit lazy val timeout = Timeout(5 seconds)
}
我想访问ExecutionContext
像这样的另一个特征
trait AsyncGoodness extends NonBlockingGoodness {
import akka.dispatch.Future
def doSomething = {
Future { "Future is the bomb." }
}
但是,我收到了错误
Could not find implicit value for parameter executor: akka.dispatch.ExecutionContext
更新:
我想出了如何获得ExecutionContext
范围
trait AsyncGoodness extends NonBlockingGoodness {
import akka.dispatch.ExecutionContext
import akka.dispatch.Future
def doSomething()(implicit executor: ExecutionContext) = {
Future { "Future is the bomb." }
}
但是,我有一个后续问题。由于我可能有不止一种方法AsyncGoodness
使用ExecutionContext
,有没有办法在trait
级别而不是像我上面所做的那样在每个方法中传递它。