我使用twitter stream api,2分钟后Twitter停止向我发送推文,但如果我停止并重新运行应用程序,则会再次向我发送数据。所以我想如果我在30秒后没有收到回复,我想重新启动流。
我的代码
ws.url(url)
.sign(OAuthCalculator(consumerKey,requestToken))
.withQueryString(("track",keywordsString))
.withMethod("POST")
.stream()
.map { response =>
Logger.info(keywordsString+" : "+response.headers.status)
if(response.headers.status == 200){
response.body
//.keepAlive ?
//.idleTimeout ?
.via(sharedKillSwitch.flow)
.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) => mentionActor ! StreamPair(keywords,tweet)
case Failure(e) => Logger.info("Failure: "+e.getMessage)
}
}
if(response.headers.status.toString.startsWith("4")){
Logger.info("Response 4XX : "+response.headers.status)
response.body
.via(sharedKillSwitch.flow)
.scan("")((acc, curr) => if (acc.contains("\r\n")) curr.utf8String else acc + curr.utf8String)
.filter(_.contains("\r\n"))
.map ( json => json )
.runForeach{
case error: String =>
Logger.info("error: "+error)
this.stopStream()
streamRestartActor ! StartStream(keywords,keywordsString)
}
}
}
解决方案:
ws.url(url)
.sign(OAuthCalculator(consumerKey,requestToken))
.withQueryString(("track",keywordsString))
.withMethod("POST")
.stream()
.map { response =>
Logger.info(keywordsString+" : "+response.headers.status)
if(response.headers.status == 200){
response.body
.via(sharedKillSwitch.flow)
.idleTimeout(30.seconds) //this
.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) => mentionActor ! StreamPair(keywords,tweet)
case Failure(e) => Logger.info("Failure: "+e.getMessage)
}
.onComplete { //this
case Success(_) =>
Logger.info("Done")
this.stopStream()
Thread.sleep(time)
streamRestartActor ! StartStream(keywords,keywordsString)
case Failure(e) =>
Logger.info("Failed with "+e.getMessage)
this.stopStream()
Thread.sleep(time)
streamRestartActor ! StartStream(keywords,keywordsString)
}
}
if(response.headers.status.toString.startsWith("4")){
Logger.info("Response 4XX : "+response.headers.status)
response.body
.via(sharedKillSwitch.flow)
.scan("")((acc, curr) => if (acc.contains("\r\n")) curr.utf8String else acc + curr.utf8String)
.filter(_.contains("\r\n"))
.map ( json => json )
.runForeach{
case error: String =>
Logger.info("error: "+error)
this.stopStream()
Thread.sleep(time)
streamRestartActor ! StartStream(keywords,keywordsString)
}
}
}