how to stop and kill the Twitter4j stream correctly

前端 未结 2 1686
离开以前
离开以前 2021-01-23 21:20

I try to use the Twitter4j streaming features. So my implementation is in the following class :

public class TweetsStream {

    private TwitterStream m_twitters         


        
相关标签:
2条回答
  • 2021-01-23 22:10

    Here's the solution :

    The method TwitterStreamFactory.getSingleton() returns the same instance of the TwitterStream.

    Even if you delete properly your TwitterStream object, the listeners are stored in your TwitterStream Singleton config... So you have to create differents instances of the TwitterStream object with :

    TwitterStreamFactory l_Twitterfactory = new TwitterStreamFactory();
    m_twitterstream = l_Twitterfactory.getInstance();
    
    0 讨论(0)
  • 2021-01-23 22:18

    I have faced the similar issue . Still any of the above solution is not working , So i did a work around for this stored the twitterStream object in a map . so i can get the same object later when i need to close the stream .

                ProfileEntity profileEntity = profileRepository.findBySocialId( twitterId+"" ) ;
                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true);
                cb.setOAuthConsumerKey( twitterCustomerKey );
                cb.setOAuthConsumerSecret( twitterCustomerSecret );
                cb.setOAuthAccessToken( profileEntity.getAccessToken() ) ; 
                cb.setOAuthAccessTokenSecret( profileEntity.getAccessTokenSecret() ) ; 
                twitterStream =  new TwitterStreamFactory(cb.build()).getInstance();
    
              twitterStreamMap.put( twitterId , twitterStream ) ;
    
    0 讨论(0)
提交回复
热议问题