使用twitter4j在运行时添加位置过滤器

问题描述 投票:0回答:1

我正在使用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)
java twitter twitter-hbc
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.