Akka RoundRobinPool文件计数器

时间:2015-04-23 09:43:05

标签: java multithreading directory akka round-robin

我想总结一个目录+大小的文件。我想使用akka multithreaded(路由器大小为5)。不幸的是,我不是。

MyUntypedActor:计算各个目录

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;

        for (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);
    }


}


}

生成循环池并接收结果的主类

    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.");
    } finally {
        // 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 + ")";
}
}

0 个答案:

没有答案