Playframework - 停止流连接

时间:2016-08-21 15:18:32

标签: scala playframework akka akka-stream twitter-streaming-api

我正在向Twitter流式api发送多个请求以跟踪单词,我希望在某个时间点停止跟踪某些单词,如何关闭流连接?

我尝试过使用SharedKillSwitch(stream-dynamic),但没有停止连接。

val sharedKillSwitch = KillSwitches.shared(name)

val request = ws.url(url)
.sign(OAuthCalculator(consumerKey, requestToken))
.withQueryString(("track",query))
.withMethod("POST")
.stream()
.map { response =>
  if(response.headers.status == 200){
    response.body
      .scan("")((acc, curr) => if (acc.contains("\r\n")) curr.utf8String else acc + curr.utf8String)
      .filter(_.contains("\r\n"))
      .map(json => Try(parse(json).extract[Tweet]))
      .runForeach {
        case Success(tweet) =>
          println("-----")
          println(tweet.text)
        case Failure(e) =>
          println("-----")
          println(e.getStackTrace)
      }
  }
}

Source(Stream(request)).via(sharedKillSwitch.flow)
sharedKillSwitch.shutdown()

0 个答案:

没有答案