使用 akka http -> java.lang.ClassNotFoundException的运行时错误: akka.stream.Attributes$CancelStrategy$Str



我在现有的akka流集成中尝试设置示例akka-http集成时遇到了这个错误。

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)

知道是什么原因导致这个运行时类找不到问题吗?

我正在使用";10.1.14";akkahttp和";2.5.31";scala 2.12.15 上的akka/akka流库

问题在于bindingFuture的定义。它被定义为val,需要是一个惰性val。在App对象初始化结束之前,我不会调用bindingFuture。我还让requestHandler定义变得懒惰,这样整个部分初始化得很晚,而不是在实例化后立即初始化。

这允许在调用绑定之前初始化所有先决条件管道。

相关内容

最新更新