【问题标题】:Akka HTTP Routing DSL example is invalidAkka HTTP 路由 DSL 示例无效
【发布时间】:2020-11-29 14:51:33
【问题描述】:

所以我正在尝试构建一个最小的 HTTP 服务器来接收 POST 请求,但我无法完成它,因为不知何故 Akka docs 上的示例无效,我无法从任何地方获取 executionContext

这是我的代码:

object Main extends App {

  implicit val system: ActorSystem = ActorSystem("server-system")
  implicit val executionContext = system.executionContext  // can't resolve symbol executionContext

  // test route
  val route =
    path("test") {
      get {
        complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, "test"))
      }
    }

  val address = "localhost"
  val port = 8080
  val bindingFuture = Http().newServerAt(address, port).bind(route)
  println(s"Server online at $address:$port\nPress RETURN to stop...")

  StdIn.readLine()
  bindingFuture
    .flatMap(_.unbind())                  // no implicits found for parameter
    .onComplete(_ => system.terminate())  // executor: ExecutorContext

}

我正在使用 10.2.1 版本,我正在使用 gradle 导入,如下所示:

compile group: 'com.typesafe.akka', name: "akka-http_2.12", version: "10.2.1"
compile group: 'com.typesafe.akka', name: "akka-stream_2.12", version: "2.6.8"

您是否有任何其他有效的快速入门示例,或任何其他更易于使用的库,以便像我尝试的那样设置一个非常小的服务器?

【问题讨论】:

    标签: scala server akka dsl akka-http


    【解决方案1】:

    在 Scala 中定义ExecutionContext 的方法有很多种。例如,其中 2 个是:

    implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
    

    或:

    implicit val ec: ExecutionContext = system.dispatcher
    

    但是在选择你真正想要使用的之前,我建议阅读What is execution context in Scala?

    您还可以阅读 Akka 的 Dispatchers,了解更多可定制的选项。还有Execution Context and Dispatcher - Best practices, useful configurations and Documentation的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多