I believe that I have a solution for you, and this is not connected with the accumulation of a whole group of Future s. Firstly, the concept of a high level. Two participants are participating in this thread. First we will call FilesProcessor . This actor will be short-lived and wealthy. Whenever you want to process multiple files sequentially, you create an instance of this actor and send it a message containing the names (or paths) of the files you want to process. When he has finished processing all the files, he stops. The second actor will be called LineProcessor . This stateless actor lived for a long time and united behind a router. It processes the line of the file and then responds to the one who requested the processing of the line, saying that it has finished processing this line. Now on to the code.
Posts first:
public class Messages { public static class ProcessFiles{ public final List<String> fileNames; public ProcessFiles(List<String> fileNames){ this.fileNames = fileNames; } } public static class ProcessLine{ public final String line; public ProcessLine(String line){ this.line = line; } } public static class LineProcessed{} public static LineProcessed LINE_PROCESSED = new LineProcessed(); }
And FilesProcessor :
public class FilesProcessor extends UntypedActor{ private List<String> files; private int awaitingCount; private ActorRef router; @Override public void onReceive(Object msg) throws Exception { if (msg instanceof ProcessFiles){ ProcessFiles pf = (ProcessFiles)msg; router = ...
And LineProcessor :
public class LineProcessor extends UntypedActor{ @Override public void onReceive(Object msg) throws Exception { if (msg instanceof ProcessLine){ ProcessLine pl = (ProcessLine)msg;
The line processor now sends the response back without additional content. You could, of course, change this if you needed to send something back based on line processing. I'm sure this code is not bulletproof, I just wanted to show you a high-level concept on how you can execute this thread without request / response semantics and Future s.
If you have any questions about this approach or would like more information, let me know and I would be happy to provide it.
source share