java - Akka RoundRobinPool File counter -
i sum files in directory + size . use akka multithreaded ( router size 5 ) . unfortunately, i'm not.
myuntypedactor: calculation of individual directories
public class myuntypedactor extends untypedactor { private long length = 0; private int amountfiles = 0; public static props getprops() { return props.create(myuntypedactor.class); } @override public void onreceive(object msg) throws exception { if(msg instanceof dirstats) { this.amountfiles += ((dirstats) msg).filecount; this.length += ((dirstats) msg).totalsize; } else { unhandled(msg); } list<future<object>> results = new arraylist<future<object>>(); // prüfe ob die erhaltene nachricht vom korrekten typ if (msg instanceof file) { file dirpath = (file)msg; (file file : dirpath.listfiles()) { if (file.isfile()) { length += file.length(); amountfiles++; } else { actorref mysubuntypedactor = getcontext().actorfor("/user/myactor"); results.add(patterns.ask(mysubuntypedactor, file, 10000)); } } future<iterable<object>> alldone = futures.sequence(results, getcontext().dispatcher()); alldone.onsuccess(new onsuccess<iterable<object>>() { @override public void onsuccess(iterable<object> objects) throws throwable { system.out.println(objects); getsender().tell(new dirstats(amountfiles, length), getself()); } }, getcontext().dispatcher()); getsender().tell(new dirstats(amountfiles, length), getself()); } else { unhandled(msg); } } }
main class generate round-robin pools , receive results
public class actordirsize { public dirstats dirstats(actorsystem system, file dir) throws ioexception { actorref myuntypedactor = system.actorof(new roundrobinpool(5).props(myuntypedactor.getprops()), "myactor"); future<object> reply = patterns.ask(myuntypedactor, dir, 10000); try { object result = await.result(reply, duration.create(2, timeunit.seconds)); dirstats dirstats = (dirstats) result; return dirstats; } catch (exception e) { e.printstacktrace(); } return null; } public static void main(string[] args) throws ioexception { // bricht ab, wenn dem programm keine argumente mitgegeben werden if (args.length<1) { system.out.println("benötigter parameter: startverzeichnis"); system.exit(1); } // prüft, ob parameter ein korrekter pfad ist. file startdir = new file(args[0]); if (!startdir.isdirectory()) { system.out.println("dies ist kein verzeichnis!"); system.exit(1); } actordirsize test = new actordirsize(); // erstellt einen neuen aktor actorsystem system = actorsystem.create("actors"); try { // ausgabe der ergebnisse dirstats result = test.dirstats(system, startdir); system.out.println(result.filecount + " dateien, " + result.totalsize + " bytes."); } { // fährt den aktor herunter system.shutdown(); } } }
dirstats (akka messages):
public class dirstats { final int filecount; final long totalsize; public dirstats() { filecount = 0; totalsize = 0; } public dirstats(int filecount, long totalsize) { this.filecount = filecount; this.totalsize = totalsize; } @override public string tostring() { return "dirstats(filecount=" + filecount + ", totalsize=" + totalsize + ")"; } }