我正在尝试按照说明创建Play Framework异步控制器。到目前为止,我的代码只是cut & paste from the Play documentation:
package controllers
import akka.actor.ActorSystem
import com.google.inject.Inject
import play.api.libs.concurrent.CustomExecutionContext
import play.api.mvc.{AbstractController, ControllerComponents}
import scala.concurrent.{ExecutionContext, Future}
trait MyExecutionContext extends ExecutionContext
class MyExecutionContextImpl @Inject()(system: ActorSystem)
extends CustomExecutionContext(system, "my.executor") with MyExecutionContext
class FooController @Inject() (myExecutionContext: MyExecutionContext, cc:ControllerComponents) extends AbstractController(cc) {
def foo = Action.async(
Future {
// Call some blocking API
Ok("result of blocking call")
}(myExecutionContext)
)
}
当我尝试运行这个新控制器时,出现以下错误:
ProvisionException:无法配置,请参阅以下错误:
1) No implementation for controllers.MyExecutionContext was bound.
while locating controllers.MyExecutionContext
for the 1st parameter of controllers.FooController.<init>(FooController.scala:14)
while locating controllers.FooController
for the 4th parameter of router.Routes.<init>(Routes.scala:33)
while locating router.Routes
while locating play.api.inject.RoutesProvider
有人可以解释这里可能出现的问题吗?
答案 0 :(得分:1)
该异常表示您尚未将实现(MyExecutionContext
)绑定到模块中的特征(MyExecutionContextImpl
)。
试试这个:
class Module extends AbstractModule {
override def configure(): Unit = {
bind(classOf[MyExecutionContext])
.to(classOf[MyExecutionContextImpl])
}
}
但是我从未使用过你的方法。我只使用默认的执行上下文,如:
class FooController @Inject()()(implicit val ec: ExecutionContext)
答案 1 :(得分:0)
上一个答案无效。尝试使用@ImplementedBy
(来源:https://stackoverflow.com/a/53162884/4965515)
此外,您将需要在conf / application.conf中配置执行程序。例如,尝试添加:
my.executor {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
core-pool-size-factor = 10.0
core-pool-size-max = 10
}
}