使用akka http ->java.lang时发生运行时错误,akka.stream.属性$取消策略$策略

nc1teljy  于 2022-11-05  发布在  Java
关注(0)|答案(1)|浏览(88)

我得到这个错误试图设置一个样本akka http集成在我现有的akka流集成。

package ui

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import scala.concurrent.ExecutionContext
import scala.io.StdIn

object Main extends App {

    implicit val system = ActorSystem("lowlevel")
    // needed for the future map/flatmap in the end
    implicit val executionContext: ExecutionContext = system.dispatcher
    implicit val materializer: ActorMaterializer = ActorMaterializer()

    val requestHandler: HttpRequest => HttpResponse = {
      case HttpRequest(GET, Uri.Path("/"), _, _, _) =>
        HttpResponse(entity = HttpEntity(
          ContentTypes.`text/html(UTF-8)`,
          "<html><body>Hello world!</body></html>"))

      case HttpRequest(GET, Uri.Path("/ping"), _, _, _) =>
        HttpResponse(entity = "PONG!")

      case HttpRequest(GET, Uri.Path("/crash"), _, _, _) =>
        sys.error("BOOM!")

      case r: HttpRequest =>
        r.discardEntityBytes() // important to drain incoming HTTP Entity stream
        HttpResponse(404, entity = "Unknown resource!")
}

    val bindingFuture = Http().newServerAt("localhost", 8080).bindSync(requestHandler)
    println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
    StdIn.readLine() // let it run until user presses return
    bindingFuture
      .flatMap(_.unbind()) // trigger unbinding from the port
      .onComplete(_ => system.terminate()) // and shutdown when done

}

代码编译得很好,但是当我在具有端点设置的顶级类上运行测试时,我得到了以下运行时错误:

java.lang.ClassNotFoundException: akka.stream.Attributes$CancellationStrategy$Strategy
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)

你知道是什么原因导致了这个运行时类找不到的问题吗?
我使用的是akka http的“10.1.14”和scala 2.12.15上的akka /akka流库的“2.5.31

im9ewurl

im9ewurl1#

问题出在bindingFuture的定义上。它被定义为一个瓦尔,并且需要是一个延迟val。我在App对象初始化结束之前不会调用bindingFuture。我还将requestHandler定义也做了延迟,这样整个片段在示例化之后会延迟初始化,而不是立即初始化。
这允许在调用绑定之前初始化所有的先决条件管道。

相关问题