1

我正在使用 Twitter 流对某些过滤后的推文进行采样。但是,我只想提取一小组推文,因此我使用时间来控制何时关闭流。同时我写一个文件。但是,在我退出程序之前,流会继续并且不会关闭。有什么理由吗?我正在使用 scala 来执行此操作,这是我的代码:

def simpleStatusListener = new StatusListener() {
    def onStatus(status: Status) { 
      appendToFile("/tmp/stream.txt",status.getText) 
    }
    def onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {}
    def onTrackLimitationNotice(numberOfLimitedStatuses: Int) {}
    def onException(ex: Exception) { ex.printStackTrace }
    def onScrubGeo(arg0: Long, arg1: Long) {}
    def onStallWarning(warning: StallWarning) {}
    }

val twitter = new TwitterFactory().getInstance

twitterStream.addListener(simpleStatusListener)

val now = System.nanoTime
if((System.nanoTime-now)/1000000 > 10){
      twitterStream.cleanUp
      twitterStream.shutdown
    }
4

1 回答 1

2

now关闭流的位可能会在计算变量的行之后立即执行。因此,if 条件不太可能(System.nanoTime-now)/1000000 > 10成立。这是一个解决方案:

def simpleStatusListener = new StatusListener() {
  def onStatus(status: Status) { 
    appendToFile("/tmp/stream.txt",status.getText) 
  }
  def onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {}
  def onTrackLimitationNotice(numberOfLimitedStatuses: Int) {}
  def onException(ex: Exception) { ex.printStackTrace }
  def onScrubGeo(arg0: Long, arg1: Long) {}
  def onStallWarning(warning: StallWarning) {}
}

val twitter = new TwitterFactory().getInstance

twitterStream.addListener(simpleStatusListener)

//scheduling the closing of the stream
val interval = 10 * 1000000

val timer = new Timer() 

val cancelTask = new TimerTask {
  def run() {
    twitterStream.cleanup()
    twitterStream.shutdown()
  }
}

timer.schedule(cancelTask, interval)
于 2013-04-08T08:03:24.267 回答