Greetings, 

I am trying to glue a play controller together with an Actor System in a 
large legacy codebase. Reimplementing the whole codebase as a raw actor 
system is not viable in the short term so it will have to take over piece 
by piece. What I am wondering is if the following paradigm is viable or if 
there is some issue with the concept. 

package actors;

import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;

import java.util.concurrent.CompletableFuture;

public class PerRequestActor extends UntypedActor {
    private final CompletableFuture<FinalResponse> future;
    private ResponseA a;
    private ResponseB b;
    private ResponseC c;

    public static Props props(final CompletableFuture<FinalResponse> future) {
        return Props.create(PerRequestActor.class, future);
    }

    public static CompletableFuture<FinalResponse> invoke(final ActorSystem 
system) {
        final CompletableFuture<FinalResponse> future = new 
CompletableFuture<>();
        system.actorOf(props(future));
        return future;
    }

    public PerRequestActor(final CompletableFuture<FinalResponse> future) {
        this.future = future;
    }

    @Override
    public void preStart() throws Exception {
        context().system().actorSelection("/user/A").tell(new RequestA(), 
getSelf());
        context().system().actorSelection("/user/B").tell(new RequestB(), 
getSelf());
        context().system().actorSelection("/user/C").tell(new RequestC(), 
getSelf());
    }

    @Override
    public void onReceive(final Object message) throws Exception {
        if (message instanceof ResponseA) {
            a = (ResponseA) message;
            completeIfPossible();
        } else if (message instanceof ResponseB) {
            b = (ResponseB) message;
            completeIfPossible();
        } else if (message instanceof ResponseC) {
            c = (ResponseC) message;
            completeIfPossible();
        } else {
            unhandled(message);
        }
    }

    public void completeIfPossible() {
        if (a != null && b != null && c != null) future.complete(new 
FinalResponse(a, b, c));
        getContext().stop(getSelf());
    }

    public static class FinalResponse {
        public FinalResponse(final ResponseA a, final ResponseB b, final 
ResponseC c) {
            // Code Here
        }
    }

    public static class RequestA { }
    public static class ResponseA { }
    public static class RequestB { }
    public static class ResponseB { }
    public static class RequestC { }
    public static class ResponseC { }
}




The idea is that the user calls invoke synchronously and then that spins up 
the actor system passing the future to the actor. In the pre-start the 
messages are sent to other actors via tell and then when the actor collects 
all of the information it needs to finish the task, it collects the 
information, creates the final response and completes the future.

The question is if this is a viable concept. 

Thanks for your advice.

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to