50

我正在调用这样的网络服务:

WS
  .url(url)
  .get
  .map { response => // error occurs on this line
    response.status match {
      case 200 => Right(response.json)
      case status => Left(s"Problem accessing api, status '$status'")
  }
}

完整的错误:Error: Cannot find an implicit ExecutionContext, either require one yourself or import ExecutionContext.Implicits.global

4

2 回答 2

114

根据这个问题,它在文档中得到了修复。我需要添加以下导入:

import play.api.libs.concurrent.Execution.Implicits._
于 2013-02-19T15:56:27.057 回答
2

从 Play 2.6 开始,建议对execution context.

默认执行上下文注入:

Foo.scala

class Foo @Inject()()(implicit ec:ExecutionContext) {

def bar() = {
   WS.url(url)
     .get
     .map { response => // error occurs on this line
       response.status match {
         case 200 => Right(response.json)
         case status => Left(s"Problem accessing api, status '$status'")
     }
   }
}

自定义执行上下文注入:

应用程序.conf

# db connections = ((physical_core_count * 2) + effective_spindle_count)
fixedConnectionPool = 9

database.dispatcher {
  executor = "thread-pool-executor"
  throughput = 1
  thread-pool-executor {
    fixed-pool-size = ${fixedConnectionPool}
  }
}

DatabaseExecutionContext.scala

@Singleton 
class DatabaseExecutionContext @Inject()(system: ActorSystem) extends CustomExecutionContext(system,"database.dispatcher")

Foo.scala

class Foo @Inject()(implicit executionContext: DatabaseExecutionContext ) {   ...    }

更多信息在:

https://www.playframework.com/documentation/2.6.x/Migration26#play.api.libs.concurrent.Execution-is-deprecated https://www.playframework.com/documentation/2.6.x/Highlights26#CustomExecutionContext -and-Thread-Pool-Sizing

于 2016-05-16T23:25:17.713 回答