【问题标题】:Shut down akka system with spray from non akka system使用来自非 akka 系统的喷雾关闭 akka 系统
【发布时间】:2016-04-19 14:49:32
【问题描述】:

我对 scala 中的 akka 和期货非常陌生。我使用 spray 来获取 URL 的输出并将 Future[String] 返回给另一个对象。这是发出 HTTP 请求的对象。

object ActionsService {

  private implicit val formats = DefaultFormats
  implicit val system = ActorSystem()
  import system.dispatcher

  val pipeline = sendReceive ~> unmarshal[String]
  def getActions: Future[String] ={
     val out = getOutput("http://www.google.com")
    out
  }


  def getOutput(url: String): Future[String] ={
    val response = pipeline (Get (url) )
    response
  }

  def shutdown(code: Int): Unit = {
    IO(Http).ask(Http.CloseAll)(1.second).await
    system.shutdown()
  }
}

这是我试图关闭actor系统的另一个对象中的主要方法。

import ExecutionContext.Implicits.global
def main(args: Array[String]) {
  val test = ActionsService.getActions
test.onComplete {
  case Success(x) => println(x)
  case Failure(y) => println(y)
}
  ActionsService.system.shutdown()

由于某种原因,akka 系统不会关闭。我还尝试使用我的 shutdown 方法关闭 akka 系统,但我得到一个 Exception in thread "main" akka.pattern.AskTimeoutException: Timed out 错误(当调用 shutdown 方法时,这也发生在 main 方法中)。

akka 版本是 2.2.3

【问题讨论】:

    标签: scala akka spray


    【解决方案1】:

    您没有指定您正在使用的 Akka 版本,但在上一个版本中您需要调用

    system.terminate()
    

    而是等待这样的终止(作为示例)

    Await.ready(system.whenTerminated, Duration.Inf)
    

    【讨论】:

    • akka 版本是 2.2.3,我尝试了 ActionsService.system.awaitTermination() 似乎永远不会终止..
    • 这些方法在 2.2.3 中也不存在
    【解决方案2】:

    上面的答案对我没有用,所以我不得不在调用shutdown方法之前为main方法添加一个休眠期来阻塞线程。

    def main(args: Array[String]) {
      val test = ActionsService.getActions
      Thread.sleep(700)
    test.onComplete {
      case Success(x) => println(x)
      case Failure(y) => println(y)
    }
    CorporateActionsService.system.shutdown()
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2023-03-16
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 2015-05-27
      相关资源
      最近更新 更多