【问题标题】:adding location filter at runtime using twitter4j使用 twitter4j 在运行时添加位置过滤器
【发布时间】:2014-06-28 10:40:24
【问题描述】:

我正在使用 twitter4j 从 twitter 获取按位置过滤的流,使用以下方法:

    List<Location> locations = new ArrayList<>;

    StatusesFilterEndpoint endpoint.locations(locations);
    BasicClient client = new ClientBuilder()
            .name(NAME)
            .hosts(Constants.STREAM_HOST)
            .endpoint(endpoint)
            .authentication(auth)
            .processor(new StringDelimitedProcessor(rowTweet))
            .build(); 
   client.connect();


   while (isConnected()) {
                String rowtweet = rowTweet.poll(5, TimeUnit.SECONDS);
                if (rowtweet != null) {
                    Status status = DataObjectFactory.createStatus(rowtweet);
                    statusQueue.put(status);
                }
            }

这工作正常并根据位置过滤状态,我从这个位置获得状态,但是我需要在运行时删除或添加位置,这可能吗?

我尝试使用添加位置,但出现以下异常:

 public void addLocation(Location target) throws InterruptedException, TwitterException        {
    locations.add(target);
    endpoint.locations(locations);
    connect();
} 

这个例外:

java.lang.IllegalStateException: There is already a connection thread running for TweetsByLocationStream, endpoint: /1.1/statuses/filter.json?delimited=length&stall_warnings=true
        at com.twitter.hbc.httpclient.BasicClient.connect(BasicClient.java:98)
        at com.newsry.streams.TweetsByLocationStream.connect(TweetsByLocationStream.java:77)
        at com.newsry.streams.TweetsByLocationStream.addLocation(TweetsByLocationStream.java:129)
        at com.newsry.engine.NewsryEngine.addLocation(NewsryEngine.java:183)
        at com.newsry.streams.LocationStreamTest.main(LocationStreamTest.java:44)

【问题讨论】:

  • 您是否尝试过先停止(),然后添加位置(),最后连接()或重新连接()?
  • 谢谢,但这是我已经做的,stop() 然后 addLocation() 然后 reconnect() 失败了,我尝试在没有 stop() 的情况下添加位置,它工作正常,我会用正确的代码重播。

标签: java twitter twitter-hbc


【解决方案1】:

您只需停止TwitterStream 并通过添加或删除您想要的位置来更改您的FilterQuery。然后你需要重启TwitterStream。它几乎是运行时的,但它可以工作(它在非常小的时间间隔内完成,所以你不会丢失twitter4j 流结果)。

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2016-11-07
    • 2015-10-15
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多