score:4
You should either forward
the message to the slaves (which keeps the same sender), or include the sender in the message to the slaves. For example:
def receive = {
case Calculate => slave ! CalculateWithSender(sender)
case res @ Result(value, originalSender) =>
val result = resultMap(originalSender) + res // assuming results are just added
resultMap += (originalSender -> result)
if (resultsAreFinished(originalSender))
originalSender ! result
}
or
def receive = {
case Calculate => slave.forward(Calculate)
case res @ Result(value, originalSender) =>
val result = resultMap(originalSender) + res // assuming results are just added
resultMap += (originalSender -> result)
if (resultsAreFinished(originalSender))
originalSender ! result
}
or
def receive = {
case Calculate => slave.tell(Calculate, sender)
case res @ Result(value, originalSender) =>
val result = resultMap(originalSender) + res // assuming results are just added
resultMap += (originalSender -> result)
if (resultsAreFinished(originalSender))
originalSender ! result
}
I'm not familiar with Play promises, but ?
(ask
) returns a Akka Future
. If .asPromise
converts an Akka Future
to a Play Promise
then you're set.
score:1
Spin up a new actor for each calculation. forward the Calculate message to the new actor. The new actor stores the original sender. When the result is ready, the result is sent to the original sender, and the ephemeral actor dies:
class CalculateActor extends Actor {
var origSender : ActorRef = _
def receive {
case Calculate => {
origSender = sender
slaveActors ! doStuff
//....
}
case Result(value) => {
if(results are ready) {
origSender ! results
self ! PoisonPill // I'm done, time to die
}
}
}
class MasterActor extends Actor {
def receive {
case msg @ Calculate => {
// forward sends without changing the sender
context.actorOf(Props(new CalculateActor)).forward(msg)
}
}
}
Source: stackoverflow.com
Related Query
- How to get a Promise (or Future) by asking an Akka Actor considering that the result is NOT available as a response from the same message
- How can I get the name of an Akka actor from within the actor itself?
- How do I get the CPU utilization of an akka actor
- How to get a reference to the actor instance that runs a function?
- How can I get the ActorRef from the Receptionist so my actor can send messages to that actor
- how to get the value of a future in an Async way in scala test using akka test kit
- How to get Akka actor by name as an ActorRef?
- How is "become" implemented in languages that support the actor model?
- How to get the actor system reference from inside the actor
- How to test that Akka actor was created in Scala
- How do I test an Akka actor that sends a message to another actor?
- How do I get the absolute remote actor url from inside the actor?
- Invoking a Future inside a receive method and stopping the actor after that
- How to stop gracefully the actor system for an akka-http server that must be deployed.
- Akka Actor how to only process the latest message
- How to unit test an Akka actor that sends a message to itself, without using Thread.sleep
- How to get list of traits that were mixed in the specified class?
- How to create an Akka Actor given the class name
- In scala, how do you transform a list of futures into a future that returns the first successful future?
- How can I compile only the Spark Core and Spark Streaming (so that I can get unit test utilities of Streaming)?
- What happens to the future that has been spawned off by my actor that restarts
- How to get the row from a dataframe that has the maximum value in a specific column?
- In scala, how can I get the count of elements that never shown in both arrays?
- How can I resolve conflicting actor systems while testing akka-http and akka actors at the same spec file?
- How to test Akka actor that spawn child actors at runtime?
- Scala akka typed: how to get ActorRef to Actor from it's instance and send message itself?
- Scala macros: How can I get a list of the objects within a given package that inherit some trait?
- How would you change this Akka Streams example to get the materialized value Future[ServerBinding]?
- How should an akka actor be created that might throw an exception?
- Get underlying Actor from ActorRef with the Akka Test Kit
More Query from same tag
- How to avoid scala's case class default toString function being overridden?
- Parse a json array of object to their appropriate case class
- How to write the function isFunction in scala?
- Looking for examples for structures that apply some but not all properties of a Monad
- How to remove footer from file while reading file in spark scala
- How do I render a SHtml.ajaxButton with 'onClick' action and href link in a Lift Snippet
- Is it possible to register an UDF that has generic return type in Scala
- Trying to make sierpinski triangle generator in a functional programming style
- How to avoid return in scala
- How to do a self cartesian product over the different partitions of a Spark dataset?
- How to get list of fields and subfields of a Scala case class
- ReactiveMongo plugin play framework seems to restart with every query
- Scala simple dummy project
- Spark use self reference in calculation for column
- How to use scala trait with `self` reference?
- Why am I getting this timeout during unit test of akka-stream?
- Scala Aggregate result from multiple Future calls
- If condition in map function
- When should an Actor be created in the Actor System vs Actor Context?
- Spark: Reading Avro messages from Kafka using Spark Scala
- How to initialize a final optional field in Scala?
- Manually creating actor hierarchy in akka
- what makes a variable be visible (intellij idea)
- How can I iterate Spark's DataFrame rows?
- Large matrix operations: Multiplication in Scala/Apache Spark
- How to extract div having no class using jsoup?
- Recommended way to interact with MySQL in Play framework (scala)?
- aggregate function Count usage with groupBy in Spark
- Spark: remove all duplicated lines
- Spark generate Sub Dataframes from a super Dataframe Optimized approach