如何在具有超时的单独调度程序上运行Akka Streams图形?

时间:2017-01-15 20:51:49

标签: scala akka akka-stream akka-http

这个问题基于我做过的宠物项目和这个SO主题。在Akka HTTP路由定义中,我启动了一个长时间运行的进程,当然我想在不阻塞用户的情况下这样做。我可以使用下面的代码片段来实现这一目标:

blocking-io-dispatcher {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 16
  }
  throughput = 1
}
complete {
  Try(new URL(url)) match {
    case scala.util.Success(u) => {
      val src = Source.fromIterator(() => parseMovies(u).iterator)

      src
        .via(findMovieByTitleAndYear)
        .via(persistMovies)
        .toMat(Sink.fold(Future(0))((acc, elem) => Applicative[Future].map2(acc, elem)(_ + _)))(Keep.right)
        // run the whole graph on a separate dispatcher
        .withAttributes(ActorAttributes.dispatcher("blocking-io-dispatcher"))
        .run.flatten
        .onComplete {
          _ match {
            case scala.util.Success(n) => logger.info(s"Created $n movies")
            case Failure(t) => logger.error(t, "Failed to process movies")
          }
        }

      Accepted
    }
    case Failure(t) => logger.error(t, "Bad URL"); BadRequest -> "Bad URL"
  }
}

如果我已经解决了问题,那么问题是什么?问题是我不确定如何设置超时。图表的执行会创建一个Future,直到专用blocking-io-dispatcher完成为止。如果我添加Await调用,代码会阻塞。有没有办法暂停?

1 个答案:

答案 0 :(得分:2)

completionTimeout阶段应该有所帮助。示例如下:

src
    .completionTimeout(5.seconds)
    ...
    .run.flatten
    .onComplete {
        case scala.util.Success(n) => logger.info(s"Created $n movies")
        case Failure(t: TimeoutException) => logger.error(t, "Timed out")
        case Failure(t) => logger.error(t, "Failed to process movies")
    }

文档参考here