Akka, how can i preserve the sender reference when working with simple Java object and Futures?

StackOverflow https://stackoverflow.com/questions/22808521

  •  26-06-2023
  •  | 
  •  

Question

I have a Java class which creates a supervisor Actor which then creates a child actor for each request it needs to generate.

  1. Java class uses a Future to get response back from supervisor Actor
  2. Supervisor uses tell and sender reference to get response back from child Actor

However my supervisor Actor is a singleton scoped bean so i need to be able find a way to store the sender reference each time my Java class makes a request to the supervisor. What is the best way to do this?

Était-ce utile?

La solution

I don't think singleton scope is a good isea, as Akka needs to be able to restart actors when exceptions occur. That might lead to some weird problems. We used prototype scope ourselves.

Other than that, one possibility is to simply decouple receiving the request and sending the response by passing the server through your actors, you just use forward instead of tell, and the last actor in your pipeline will respond to the sender. This way the supervisor does not need to care about the response. Obviously this is ideal if your supervisor does nothing but sending the response to the sender.

If there is some processing to be done before sending the response, you can create a temporary actor and pass it the sender reference, and let this actor collect the results, send the response to your future and stop itself. This is especially useful if you need to wait for more than one response and aggregate it.

You can also add the sender reference to the message you send from the supervisor to your actors and back to the supervisor. Simple, yet effective.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top