我正在向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()