java - Where do you close a writer in TwitterStream? -


i'm working on compiling bunch of tweets information retrieval class. i'm trying using both rest api , streaming api through twitter4j. when using streaming api, use following modifications this example:

final limitedfilewriter output = new limitedfilewriter("tweets","tweets"); twitterstream twitterstream = new twitterstreamfactory(cb.build()).getinstance();     statuslistener listener = new statuslistener() {         @override         public void onstatus(status status) {             try{                 output.write("@" + status.getuser().getscreenname() + " -- " + status.gettext()+"\n");             }             catch(ioexception e){                 e.printstacktrace();             }         }     }      twitterstream.addlistener(listener);     twitterstream.sample("en");     //output.close(); 

it seems can't ever close writer. writer using wraps bufferedwriter, while keeping track of file size. if file exceeds size (128mb), writer close current file , create new file. here relevant class functions:

public void write(string s) throws ioexception {     if(byteswritten + s.getbytes(charset).length >= maxsize){         output.close();         byteswritten = 0;         filenum++;         string filename = directory + "/" + basename+fmt.format(filenum);         currentfile = new file(filename);         output = new bufferedwriter             (new outputstreamwriter(new fileoutputstream(filename),charset));     }      output.write(s);     byteswritten += s.getbytes(charset).length;  }  public void close() throws ioexception{     output.close(); } 

if try close writer after twitterstream.sample() (commented out), program crashes because trying write closed file. if understanding correct, because twitterstream class creates new thread runs concurrently main thread. then, main thread closes stream , twitterstream can no longer write it.

if that's case, should close writer?

if have understood question correctly, want able turn of tweets collection @ point, close open file writers , have clean exit. achieve can use synchronized block.

final object lock = new object(); final limitedfilewriter output = new limitedfilewriter("tweets","tweets"); twitterstream twitterstream = new twitterstreamfactory(cb.build()).getinstance(); statuslistener listener = new statuslistener() {     @override     public void onstatus(status status) {         try{         output.write("@" + status.getuser().getscreenname() + " -- " + status.gettext()+"\n");         // free lock         if (some_condition_like_i_have_enough_files) {             synchronized (lock) {                 lock.notify();             }         }         catch(ioexception e){             e.printstacktrace();         }     } }  twitterstream.addlistener(listener); twitterstream.sample("en");  try {     synchronized (lock) {         lock.wait();     } } catch (interruptedexception e) {     e.printstacktrace(); } // close twitterstream // close writer 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -