Scala twitter4s:如何在游戏框架中从Action.async返回future
我正在使用和 我正在获取发送给参与者的tweet,以便对其执行一些处理,参与者需要将响应发送回控制器操作 这是我的密码Scala twitter4s:如何在游戏框架中从Action.async返回future,scala,playframework,twitter4j,playframework-2.4,twitter4s,Scala,Playframework,Twitter4j,Playframework 2.4,Twitter4s,我正在使用和 我正在获取发送给参与者的tweet,以便对其执行一些处理,参与者需要将响应发送回控制器操作 这是我的密码 def totalTweetCount = Action.async { log.debug("in the action tofaltweets") def getTweet: PartialFunction[StreamingMessage, Unit] = { case tweet: Tweet => fu
def totalTweetCount = Action.async {
log.debug("in the action tofaltweets")
def getTweet: PartialFunction[StreamingMessage, Unit] = {
case tweet: Tweet =>
future = ask(myActor, TotalNumberOfTweets(tweet)).mapTo[Int]
}
val streaming: Future[TwitterStream] = streamingClient.sampleStatuses(stall_warnings = true)(getTweet)
}
class MyActor extends Actor {
//sends back the response to the calling code
}
现在的问题是线下连续运行(它是twitter流)并连续调用getTweet方法并将tweet对象发送给参与者
val streaming: Future[TwitterStream] = streamingClient.sampleStatuses(stall_warnings = true)(getTweet)
我想获取actor响应并在我的操作的Ok块中显示它
当我这样做时,我得到了NullPointerException
def totalTweetCount = Action.async {
log.debug("in the action tofaltweets")
def getTweet: PartialFunction[StreamingMessage, Unit] = {
case tweet: Tweet =>
var future: Future[Int] = null
future = ask(myActor, TotalNumberOfTweets(tweet)).mapTo[Int]
}
val streaming: Future[TwitterStream] = streamingClient.sampleStatuses(stall_warnings = true)(getTweet)
future.map {
result =>
Ok("Total number of tweets" + result)
}
}
如果我这样做
def totalTweetCount=Action.async{
debug(“在altweets操作中”)
}
这也是错误的
那么,正确的方法应该是什么呢?我不确定您在这里想做什么,但这里是这样的: 在所有解决方案中,您都在控制器中调用
streamingClient.sampleStatuses
方法,这没有多大意义,因为这是一种流方法。在阅读twitter4s文档时,他们似乎有一种获取推文的异步方法
现在,如果您试图接收推文、存储推文,并通过单独的方法获取推文总量,那么您应该:
调用streamingClient.sampleStatus在main
方法中使用
,并将传入的tweet发送给参与者
streamingClient.samplestatus(失速警告=true){
案例t:Tweet=>tweetsActor!t
}
这将把所有tweet数据发送到您的actor类。假设您的参与者的实现类似于
类Tweets扩展了Actor{
var tweets=ListBuffer[Tweet]()
def接收={
案例t:Tweet=>tweets+=t
case TotalTweetCount=sender()!tweets.length
}
}
您的控制器方法如下所示:
导入akka.pattern.ask_
def totalTweetCount=Action.async{
(tweetActor?TotalTweetCount).mapTo[Int]
}
这应该能奏效
def getTweet: PartialFunction[StreamingMessage, Unit] = {
case tweet: Tweet =>
var future: Future[Int] = null
future = ask(actorManager, TotalNumberOfTweets(tweet)).mapTo[Int]
future.map {
result =>
Ok("Total number of tweets" + result)
}
}
val streaming: Future[TwitterStream] = streamingClient.sampleStatuses(stall_warnings = true)(getTweet)