Issue getting the stream (TwitterStreamImpl) using Java SpringBoot + twitter4j

145 views Asked by At

A year ago, I was working on a project using twitter4j library on a Java Springboot project. When I finished the project it was working OK. But now, I would like to make some incremental developments and an unusual error has been appeared. I show the different components below.

I have been searching for the solution some days but I cannot found it. Any comment would be helpful. Thanks in advance

1. Main class:

@SpringBootApplication
public class TweetManagerApplication {
    
    public static void main(String[] args) throws TwitterException {
        SpringApplication.run(TweetManagerApplication.class, args);
    }
    
    @EventListener(ApplicationReadyEvent.class)
    public TwitterStream streamFeed() throws TwitterException {
        
        StatusListener listener = new StatusListener(){
            @Override
            public void onStatus(Status status) {
                System.out.println("%s : %s".formatted(status.getUser().getName(), status.getText()));
            }

            @Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

            @Override
            public void onScrubGeo(long userId, long upToStatusId) {}

            @Override
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
            @Override
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                // TODO Auto-generated method stub
                
            }
            @Override
            public void onStallWarning(StallWarning warning) {
                // TODO Auto-generated method stub
                
            }
        };
        TwitterStream twitterStream = Twitter.newBuilder()
                .listener(listener).build().v1().stream();
        return twitterStream.sample();
    
    }
}

2. Configuration class

@SpringBootConfiguration
@EnableAsync
public class AsyncConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncConfiguration.class);

    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        LOGGER.debug("Creating Async Task Executor");
        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("CarThread-");
        executor.initialize();
        return executor;
    }
}

3. Maven dependency

    <dependency>
        <groupId>org.twitter4j</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>4.1.1</version>

4. The last logs and the information about this issue are the following:

2023-03-08 17:22:18.980  INFO 55335 --- [][initializing]] twitter4j.TwitterStreamImpl              : Establishing connection.
2023-03-08 17:22:20.036  INFO 55335 --- [ing connection]] twitter4j.TwitterStreamImpl              : https://stream.twitter.com/1.1/statuses/sample.json?stall_warnings=true
https://stream.twitter.com/1.1/statuses/sample.json?stall_warnings=true
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=ee74468e or
    http://www.google.co.jp/search?q=9eaf216e
TwitterException{exceptionCode=[ee74468e-9eaf216e 69215812-3735082c 69215812-3735081d], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.1.1}
    at twitter4j.HttpClient.handleRequest(HttpClient.java:191)
    at twitter4j.HttpClient.request(HttpClient.java:72)
    at twitter4j.HttpClient.get(HttpClient.java:279)
    at twitter4j.TwitterStreamImpl.getSampleStream(TwitterStreamImpl.java:209)
    at twitter4j.TwitterStreamImpl$4.getStream(TwitterStreamImpl.java:180)
    at twitter4j.TwitterStreamImpl$TwitterStreamConsumer.run(TwitterStreamImpl.java:387)
2023-03-08 17:22:20.046  INFO 55335 --- [ing connection]] twitter4j.TwitterStreamImpl              : Waiting for 250 milliseconds
Caused by: java.io.FileNotFoundException: https://stream.twitter.com/1.1/statuses/sample.json?stall_warnings=true
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:2048)
    at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:2043)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:2042)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1609)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
    at twitter4j.HttpResponse.<init>(HttpResponse.java:54)
    at twitter4j.HttpClient.handleRequest(HttpClient.java:155)
    ... 5 more
Caused by: java.io.FileNotFoundException: https://stream.twitter.com/1.1/statuses/sample.json?stall_warnings=true
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1993)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
    at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:529)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:308)
    at twitter4j.HttpResponse.<init>(HttpResponse.java:39)
    ... 6 more

I am trying to use the twitter4j stream as it works a year ago

0

There are 0 answers