简体   繁体   English

检测twitter4j的关闭流

[英]Detect closing stream of twitter4j

I'm Developping an application using twitter4j. 我正在使用twitter4j开发应用程序。 I store the tweets in a file using a DAO. 我使用DAO将推文存储在文件中。

I made an implementation of the StatusListener class : 我实现了StatusListener类的实现:

public class StatusListenerStore implements StatusListener {

    // DAO
    private DAO<Status> m_statusDAO;


    // Constructor
    public StatusListenerStore(FactoryType a_factoryType,ArrayList<String> a_lanTrackList) {

        super();
        AbstractDAOFactory l_DAOFactory = AbstractDAOFactory.getFactory(a_factoryType);
        m_statusDAO = l_DAOFactory.getTweetDAO();

    }
}

The DAO called is : DAO称为:

// Extract of my class
private static HadoopFileSystem m_hadoopFileSystem = null;

public TweetHADOOPDAO(){
    m_hadoopFileSystem = new HadoopFileSystem(m_path+m_filename);
}

public void close(){
    m_hadoopFileSystem.closeWriters();
}

The HadoopFileSystem contains OutputStream i have to close. HadoopFileSystem包含我必须关闭的OutputStream

First of all, I want only ONE HadoopFileSystem object instance for all my program. 首先,我所有程序只需要一个HadoopFileSystem对象实例。 The static keyword guarantees me ? static关键字可以保证我吗?

Next, to close my outputstream, i have to call the close function in my DAO class when the stream is done. 接下来,要关闭我的输出流,必须在流完成后在DAO类中调用close函数。 I close it using the correct shutdown() function. 我使用正确的shutdown()函数将其shutdown()

But the m_statusDAO object is declared in my StatusListener class. 但是m_statusDAO对象在我的StatusListener类中声明。 How can i call my close() function in this class when the stream is close ? 当流关闭时,如何在此类中调用close()函数?

The static keyword means that the m_hadoopFileSystem variable will be shared by all instances of the class, however it does not prevent you from assigning the variable multiple times. 关键字static意味着m_hadoopFileSystem的所有实例将共享m_hadoopFileSystem变量,但是它不会阻止您多次分配该变量。 Currently you will get a new HadoopFileSystem each time you instantiate an TweetHADOOPDAO object, overwriting the reference to any existing HadoopFileSystem objects. 目前,你会得到一个新的HadoopFileSystem每次实例化一个时间TweetHADOOPDAO对象,重写引用到任何现有HadoopFileSystem对象。

If you truly need to ensure only one instance is created, take a look at using the singleton pattern . 如果您确实需要确保仅创建一个实例,请看一下使用单例模式

On your second point, unfortunately I don't think there is a shutdown event. 关于您的第二点,不幸的是,我认为没有关闭事件。 However, you could add a shutdown() method to your listener which invokes TweetHADOOPDAO#close() . 但是,您可以在调用TweetHADOOPDAO#close()侦听器中添加shutdown()方法。

Then you could additionally call StatusListenerStore#shutdown() after you invoke TwitterSteam#shutdown() . 然后,您可以在调用TwitterSteam#shutdown()之后另外调用StatusListenerStore#shutdown() TwitterSteam#shutdown()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM