每当我运行使用Future
(通过Await
,map
,onComplete
等)的结果的scala流程时,它永远不会退出,强制执行我们手动杀死进程。无论我使用extends App
还是仅使用标准def main(args: Array[String])
方法,都会发生这种情况。
在我看来,它与ThreadPoolExecutor
有关,scala会在函数结束时自动执行Future
,但我似乎无法获得处理它以关闭它。
例如,以下代码将无法退出:
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))
}
虽然这段代码退出很好(唯一的变化是返回未来的Async版本,然后等待):
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。但是,我们在以其他方式执行期货时已经看到了这一点:
val request = wsClient.url(fullUrl)
.withRequestTimeout(readTimeout)
val requestWithHeaders = headers.foldLeft(request) { (r, h) =>
r.withHeaders(h)
}
val playResponseFuture = requestWithHeaders.post(requestBody)
答案 0 :(得分:0)
我们最终找到了这个问题。 Play wsClient需要一个actor系统(从Play 2.5开始)。我们需要在退出主类之前手动终止此actor系统。退出的代码如下所示:
object ExecuteAPI extends App with StrictLogging{
try {
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 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))
} finally {
AmazonAsyncApiGatewayHelper.client.actorSystem.terminate()
}
}
答案 1 :(得分:-1)
您可以映射未来值并在最后添加Thread.sleep()
。
val result = layersResponse map { futureValue =>
Json.parse(layerResponse)
}
Thread.sleep(10000)
println(Json.prettyPrint(result))