每当我运行一个使用 a 结果的 scala 进程时Future
(通过Await
, map
,onComplete
等),它永远不会退出,迫使我们手动终止该进程。无论我使用extends App
还是仅使用标准def main(args: Array[String])
方法,都会发生这种情况。
在我看来,这与ThreadPoolExecutor
scala 将旋转以执行在Future
函数末尾挂起的 is 有关,但我似乎无法处理它来关闭它。
例如,以下代码将无法退出:
object ExecuteApi extends App with StrictLogging{
lazy val config = StratumConfiguration.setupConfiguration()
lazy val apiEndpoint = config.getProperty("APIEndpoint").split("/").head
lazy val packetApiPath = "packets/getpackets"
val resourceNames = sys.env.getOrElse("ResourceNames", "").stripMargin.split("[\n\\s]+").map(_.trim).filterNot(_.isEmpty)
val searchBody =
s"""{
| "resourceNames": [
| "${(resourceNames).mkString("\",\"")}"
| ]
|}""".stripMargin
logger.info(searchBody)
val responseFuture = AmazonAsyncApiGatewayHelper.executeHttpRequest(apiEndpoint, searchBody, Some(packetApiPath), Some("api"))
val response = Await.result(responseFuture, Duration(25, TimeUnit.SECONDS))
val layerDefinitions = Json.parse(response)
println(Json.prettyPrint(layerDefinitions))
}
虽然这段代码退出得很好(唯一的变化是返回未来的异步版本,然后等待):
object ExecuteAPI extends App with StrictLogging{
lazy val config = Configuration.setupConfiguration()
lazy val apiEndpoint = config.getProperty("APIEndpoint").split("/").head
lazy val packetApiPath = "packets/getpackets"
val resourceNames = sys.env.getOrElse("ResourceNames", "").stripMargin.split("\n").map(_.trim).filterNot(_.isEmpty)
val searchBody =
s"""{
| "resourceNames": [
| "${(resourceNames).mkString("\",\"")}"
| ]
|}""".stripMargin
logger.info(searchBody)
val layersResponse = AmazonapiGatewayHelper.executeHttpRequest(apiEndpoint, searchBody, Some(packetApiPath), Some("api"))
val layerDefinitions = Json.parse(layersResponse)
println(Json.prettyPrint(layerDefinitions))
}
中的代码AmazonAsyncApiGatewayHelper
最终通过执行 Play 库 HTTP 客户端来创建 Future。但是,我们在以其他方式执行 Futures 时也看到了这一点:
val request = wsClient.url(fullUrl)
.withRequestTimeout(readTimeout)
val requestWithHeaders = headers.foldLeft(request) { (r, h) =>
r.withHeaders(h)
}
val playResponseFuture = requestWithHeaders.post(requestBody)