twitter - Why java.util.concurrent.RejectedExecutionException using Twitter4J to sample tweets, when I restart twitterStream? -
in following java application, use twitterstream gather tweets using sample function. need start , stop stream whenever user wants, following exception:
java.util.concurrent.rejectedexecutionexception: task twitter4j.statusstreambase$1@74e75335 rejected java.util.concurrent.threadpoolexecutor@5117b235[terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2] @ java.util.concurrent.threadpoolexecutor$abortpolicy.rejectedexecution(unknown source) @ java.util.concurrent.threadpoolexecutor.reject(unknown source) @ java.util.concurrent.threadpoolexecutor.execute(unknown source) @ twitter4j.dispatcherimpl.invokelater(dispatcherimpl.java:58) @ twitter4j.statusstreambase.handlenextelement(statusstreambase.java:80) @ twitter4j.statusstreamimpl.next(statusstreamimpl.java:56) @ twitter4j.twitterstreamimpl$twitterstreamconsumer.run(twitterstreamimpl.java:568)
when user presses "crawl" or "stop crawling", method actionperformed correctly called. however, if user presses crawl , presses stop , again presses crawl, error above
i have several classes, principal ones followings:
the first 1 creates interface , comunicates crawler class.
import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextarea; public class stackov extends jframe implements actionlistener{ private jtextarea userssaved; private boolean alreadycrawling; private boolean stopreceived; private stream stream; private jbutton crawl; private jbutton stopcrawl; private mongo m; public stackov(){ this.stopreceived = false; this.alreadycrawling = false; setlayout(new flowlayout(flowlayout.center)); crawl = new jbutton("crawl"); crawl.setactioncommand("crawl"); crawl.addactionlistener(this); stopcrawl = new jbutton("stop crawling"); stopcrawl.setactioncommand("stop crawling"); stopcrawl.addactionlistener(this); m = new mongo(); //instance of class uses mongodb /* * *bla bla bla create rest of interface wish *add(button) *add(button) *etc... */ } public void setout(string out){ userssaved.settext(out); } public void setoffalreadycrawling(){ this.alreadycrawling = false; } @override public void actionperformed(actionevent e){ if(e.getactioncommand().equals("stop crawling") && !this.stopreceived){ this.stopreceived = true; stream.setstop(); } else if(e.getactioncommand().equals("crawl") && !alreadycrawling){ if(stream != null && stream.isalive()){ stream.interrupt(); } alreadycrawling = true; stream = new stream(m, this); //independently of using 1 of following 2 calls, same exception above stream.execute1(); //stream.start(); this.stopreceived = false; } } public void main(string[] args){ stackov = new stackov(); so.setsize(800, 800); so.setvisible(true); } }
the following class crawler class, shutdown twitterstream when stopcrawl true or when twitterstream has sampled number of tweets on maximum limit.
import java.awt.textarea; import java.util.arraylist; import java.util.list; import javax.swing.jtextarea; import javax.swing.jtextfield; import twitter4j.filterquery; import twitter4j.stallwarning; import twitter4j.status; import twitter4j.statusdeletionnotice; import twitter4j.statuslistener; import twitter4j.twitter; import twitter4j.twitterexception; import twitter4j.twitterfactory; import twitter4j.twitterstream; import twitter4j.twitterstreamfactory; public class stream extends thread{ private crawler cr; private twitterstream twitterstream; private int maxtweets; private int userssaved; private mongo database; private createindex ci; private twittersearch twittersearch; private static boolean stopcrawl; public stream(mongo database, twittersearch twittersearch){ stream.stopcrawl = false; this.database = database; this.cr = new crawler(database); this.twitterstream = new twitterstreamfactory(defaultconfiguration.getconfiguration()).getinstance(); this.maxtweets = 1000; ci = new createindex(database); this.twittersearch = twittersearch; } public void setstop(){ stream.stopcrawl = true; } public void execute() throws twitterexception { final list<status> statuses = new arraylist<status>(); statuslistener listener = new statuslistener() { public void onstatus(status status) { statuses.add(status); system.out.println(statuses.size() + ":" + status.gettext()); int usersindexed = cr.retrieve(status.getuser()); userssaved = database.countdocuments(); twittersearch.setout("userssaved: "+userssaved); if(usersindexed > maxtweets || stream.stopcrawl){ //ci.load(); ci.load(); //this call creates index twittersearch.setout("index created"); system.out.println("shutdown..."); twittersearch.setoffalreadycrawling(); twitterstream.shutdown(); } } public void ondeletionnotice(statusdeletionnotice statusdeletionnotice) { } public void ontracklimitationnotice(int numberoflimitedstatuses) { } public void onscrubgeo(long userid, long uptostatusid) { } public void onexception(exception ex) { ex.printstacktrace(); } @override public void onstallwarning(stallwarning arg0) { // todo auto-generated method stub } }; twitterstream.addlistener(listener); twitterstream.sample("en"); } public void execute1(){ try{ this.execute(); }catch(twitterexception e){ e.printstacktrace(); } } public void run(){ try { this.execute(); } catch (twitterexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
when thread shut down/closed, prevents being "restarted", other java io classes. in other words, once close it, can't start again. i'm pretty sure somewhere in either twitter code or code, thread being stopped. prevent happening, here's code snippet may work: http://pastebin.com/apbykuiy also, try stack overflow thingy: what cause of rejectedexecutionexception.
Comments
Post a Comment