Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream Closed error in twitter4j how to solve

it says Stream Closed error

public class StreamAPI {

    public static void main(String[] args) {


        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("xxxx");
        cb.setOAuthConsumerSecret("xxx");
        cb.setOAuthAccessToken("xx-xx");
        cb.setOAuthAccessTokenSecret("xxx");
        cb.setUseSSL(true);
        cb.setUserStreamRepliesAllEnabled(true);


        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        AccessToken accestoken = new AccessToken("xx-xx", "xxx");

        twitterStream.setOAuthAccessToken(accestoken);
        StatusListener listener = new StatusListener() {

            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }

            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            public void onScrubGeo(long userId, long upToStatusId) {
                System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

            @Override
            public void onStallWarning(StallWarning stallWarning) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        };

        FilterQuery fq = new FilterQuery();
        String keywords[] = {"France", "Germany"};

        fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);

    }

}

i am getting error

   Stream closed.
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=a8fd061d or
    http://www.google.co.jp/search?q=00070a0c
TwitterException{exceptionCode=[a8fd061d-00070a0c a8fd061d-0007099d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:199)
    at twitter4j.StatusStreamImpl.next(StatusStreamImpl.java:57)
    at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:478)
Caused by: java.io.IOException: the end of the stream has been reached
    at twitter4j.StatusStreamBase.handleNextElement(StatusStreamBase.java:88)
    ... 2 more

But the same configuration works for TwitterAPI

like image 499
jackyesind Avatar asked Jun 27 '13 05:06

jackyesind


1 Answers

Twitter gives the following reasons why you may be disconnected:

  • A client establishes too many connections with the same credentials. When this occurs, the oldest connection will be terminated. This means you have to be careful not to run two reconnecting clients in parallel with the same credentials, or else they will take turns disconnecting each other.
  • A client stops reading data suddenly. If the rate of Tweets being read off of the stream drops suddenly, the connection will be closed.
  • A client reads data too slowly. Every streaming connection is backed by a queue of messages to be sent to the client. If this queue grows too large over time, the connection will be closed.
  • A streaming server is restarted. This is usually related to a code deploy and is not very frequent.
  • Twitter's network configuration changes. These events are extremely rare, and would represent load balancer restarts or network reconfigurations, for example.

You should receive information on why you were disconnected, but not always:

Streams may be shut down for a variety of reasons. The streaming API will attempt to deliver a message indicating why a stream was closed. Note that if the disconnect was due to network issues or a client reading too slowly, it is possible that this message will not be received.

My thought would be that perhaps you're not reading data quickly enough - although I can't say for certain. Try implementing onStallWarning to see if you're getting any stall warnings, e.g.:

@Override
public void onStallWarning(StallWarning stallWarning) {
    System.out.println(stallWarning);
}
like image 104
Jonathan Avatar answered Oct 11 '22 12:10

Jonathan