Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在akka http中响应之后,scala futures是否继续运行?_Scala_Akka_Spray_Akka Http_Fire And Forget - Fatal编程技术网

在akka http中响应之后,scala futures是否继续运行?

在akka http中响应之后,scala futures是否继续运行?,scala,akka,spray,akka-http,fire-and-forget,Scala,Akka,Spray,Akka Http,Fire And Forget,我有一个用akka http编写的REST服务,它公开了一个/fire端点。当调用该端点时,我的服务应该向另一个服务发送一个文件,通常为~50MB。但是,/fire端点应该立即返回到调用方,并以fire-and-forget方式异步发送文件 我的实现如下所示 path("fire") { post { request: FireRequest => complete { sendFile(request.path) StatusCodes.O

我有一个用akka http编写的REST服务,它公开了一个/fire端点。当调用该端点时,我的服务应该向另一个服务发送一个文件,通常为~50MB。但是,/fire端点应该立即返回到调用方,并以fire-and-forget方式异步发送文件

我的实现如下所示

path("fire") {
  post { request: FireRequest =>
      complete {
        sendFile(request.path)
        StatusCodes.OK
      }
    }
  }
}

def sendFile(path: String): Future[Unit] = Future {
  // send the large file to another service
}
我测试过了,效果很好

然而,当用ASP.NET实现类似的行为时,我需要使用第三方框架(Hangfire)来处理异步任务,因为由完成的请求生成的线程最终将被终止


我的问题是:在我的akka中,httpsendFile保证在成功/失败完成之前运行,或者在某些情况下,它运行的线程将被终止?

这可能取决于您运行未来所针对的ExecutionContext

如果您使用的是全局ExecutionContext,则其行为是即使在请求完成后仍保持未来的运行


据我所知,我从未见过任何ExecutionContext会在请求完成时杀死/中断/取消未来线程。请求完成的概念在语言级别上并不存在,但与您的http层框架更相关,因此只要您不使用http层框架中的ExecutionContext,就没有理由有这样的行为。

此外,也没有故意使
scala.concurrent.Future失败的概念,还是我错过了什么?不知道你说的失败是什么意思。你确实可以把任何成功的未来变成一个失败的承诺,你也可以创造一个失败的承诺。将成功的承诺转化为失败的承诺可以有条件地进行,因此您可能能够实施某种“未来取消”(然而,这种取消是有限的,只会将当前承诺的成功转化为失败,不会传播回“来源”)