Is there a way to send values from actor to actorRef in ActorSystem
I have two actors coded as follows.
class Actor1 extends Actor
val r : ActorRef = actorOf (Props[Actor2], "master")
def receive: Receive =
case class Mul (a,b) =>
r ! CalcMul (a,b)
case class MulReply (ans) =>
println("Multiply result : " + ans)
// want to send "ans" value to testObject..
class Actor2 extends Actor
def receive: Receive =
case class CalcMul (a,b) =>
sender ! MulReply (a*b)
object testObject extends App
val a = ActorSystem("ActorSystem").actorOf(Props[Actor1], "root")
a ! CalcMul (5, 15)
// how to receive "ans" value here?
I am able to receive and print the result in Actor1 but need those values in testObject so I can use them for future operations. Cannot have a receive method in testObject as done to receive a message in Actor1 from Actor2, so cannot send them with tell method.
scala akka
add a comment |
I have two actors coded as follows.
class Actor1 extends Actor
val r : ActorRef = actorOf (Props[Actor2], "master")
def receive: Receive =
case class Mul (a,b) =>
r ! CalcMul (a,b)
case class MulReply (ans) =>
println("Multiply result : " + ans)
// want to send "ans" value to testObject..
class Actor2 extends Actor
def receive: Receive =
case class CalcMul (a,b) =>
sender ! MulReply (a*b)
object testObject extends App
val a = ActorSystem("ActorSystem").actorOf(Props[Actor1], "root")
a ! CalcMul (5, 15)
// how to receive "ans" value here?
I am able to receive and print the result in Actor1 but need those values in testObject so I can use them for future operations. Cannot have a receive method in testObject as done to receive a message in Actor1 from Actor2, so cannot send them with tell method.
scala akka
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12
add a comment |
I have two actors coded as follows.
class Actor1 extends Actor
val r : ActorRef = actorOf (Props[Actor2], "master")
def receive: Receive =
case class Mul (a,b) =>
r ! CalcMul (a,b)
case class MulReply (ans) =>
println("Multiply result : " + ans)
// want to send "ans" value to testObject..
class Actor2 extends Actor
def receive: Receive =
case class CalcMul (a,b) =>
sender ! MulReply (a*b)
object testObject extends App
val a = ActorSystem("ActorSystem").actorOf(Props[Actor1], "root")
a ! CalcMul (5, 15)
// how to receive "ans" value here?
I am able to receive and print the result in Actor1 but need those values in testObject so I can use them for future operations. Cannot have a receive method in testObject as done to receive a message in Actor1 from Actor2, so cannot send them with tell method.
scala akka
I have two actors coded as follows.
class Actor1 extends Actor
val r : ActorRef = actorOf (Props[Actor2], "master")
def receive: Receive =
case class Mul (a,b) =>
r ! CalcMul (a,b)
case class MulReply (ans) =>
println("Multiply result : " + ans)
// want to send "ans" value to testObject..
class Actor2 extends Actor
def receive: Receive =
case class CalcMul (a,b) =>
sender ! MulReply (a*b)
object testObject extends App
val a = ActorSystem("ActorSystem").actorOf(Props[Actor1], "root")
a ! CalcMul (5, 15)
// how to receive "ans" value here?
I am able to receive and print the result in Actor1 but need those values in testObject so I can use them for future operations. Cannot have a receive method in testObject as done to receive a message in Actor1 from Actor2, so cannot send them with tell method.
scala akka
scala akka
asked Nov 15 '18 at 16:09
vinayawsmvinayawsm
678624
678624
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12
add a comment |
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12
add a comment |
1 Answer
1
active
oldest
votes
As you want to receive a response from an actor you can use ask pattern for this purpose.
import akka.actor.Actor, ActorRef, ActorSystem, Props
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
case class CalsMul(a: Int, b: Int)
class Actor1 extends Actor
val r: ActorRef = context.actorOf(Props[Actor2], "master")
def receive: Receive =
case req: CalsMul =>
println("received message by Actor1")
r forward req
class Actor2 extends Actor
def receive: Receive =
case request: CalsMul =>
println("received message by Actor2")
Future.successful(request.a * request.b) pipeTo sender
object testObject extends App
implicit val system: ActorSystem = ActorSystem("ActorSystem")
val a: ActorRef = system.actorOf(Props[Actor1], "root")
implicit val timeout: Timeout = Timeout(20, SECONDS)
println(system, "sending message to Actor1")
val ans: Future[Int] = (a ? CalsMul(5, 15)).mapTo[Int] // as you are returning the multiplication of a*b
ans.foreach(println)
Note: CPU bound operations are not advised to use with actors it can have adverse effect on the performance of your application
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323500%2fis-there-a-way-to-send-values-from-actor-to-actorref-in-actorsystem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you want to receive a response from an actor you can use ask pattern for this purpose.
import akka.actor.Actor, ActorRef, ActorSystem, Props
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
case class CalsMul(a: Int, b: Int)
class Actor1 extends Actor
val r: ActorRef = context.actorOf(Props[Actor2], "master")
def receive: Receive =
case req: CalsMul =>
println("received message by Actor1")
r forward req
class Actor2 extends Actor
def receive: Receive =
case request: CalsMul =>
println("received message by Actor2")
Future.successful(request.a * request.b) pipeTo sender
object testObject extends App
implicit val system: ActorSystem = ActorSystem("ActorSystem")
val a: ActorRef = system.actorOf(Props[Actor1], "root")
implicit val timeout: Timeout = Timeout(20, SECONDS)
println(system, "sending message to Actor1")
val ans: Future[Int] = (a ? CalsMul(5, 15)).mapTo[Int] // as you are returning the multiplication of a*b
ans.foreach(println)
Note: CPU bound operations are not advised to use with actors it can have adverse effect on the performance of your application
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
add a comment |
As you want to receive a response from an actor you can use ask pattern for this purpose.
import akka.actor.Actor, ActorRef, ActorSystem, Props
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
case class CalsMul(a: Int, b: Int)
class Actor1 extends Actor
val r: ActorRef = context.actorOf(Props[Actor2], "master")
def receive: Receive =
case req: CalsMul =>
println("received message by Actor1")
r forward req
class Actor2 extends Actor
def receive: Receive =
case request: CalsMul =>
println("received message by Actor2")
Future.successful(request.a * request.b) pipeTo sender
object testObject extends App
implicit val system: ActorSystem = ActorSystem("ActorSystem")
val a: ActorRef = system.actorOf(Props[Actor1], "root")
implicit val timeout: Timeout = Timeout(20, SECONDS)
println(system, "sending message to Actor1")
val ans: Future[Int] = (a ? CalsMul(5, 15)).mapTo[Int] // as you are returning the multiplication of a*b
ans.foreach(println)
Note: CPU bound operations are not advised to use with actors it can have adverse effect on the performance of your application
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
add a comment |
As you want to receive a response from an actor you can use ask pattern for this purpose.
import akka.actor.Actor, ActorRef, ActorSystem, Props
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
case class CalsMul(a: Int, b: Int)
class Actor1 extends Actor
val r: ActorRef = context.actorOf(Props[Actor2], "master")
def receive: Receive =
case req: CalsMul =>
println("received message by Actor1")
r forward req
class Actor2 extends Actor
def receive: Receive =
case request: CalsMul =>
println("received message by Actor2")
Future.successful(request.a * request.b) pipeTo sender
object testObject extends App
implicit val system: ActorSystem = ActorSystem("ActorSystem")
val a: ActorRef = system.actorOf(Props[Actor1], "root")
implicit val timeout: Timeout = Timeout(20, SECONDS)
println(system, "sending message to Actor1")
val ans: Future[Int] = (a ? CalsMul(5, 15)).mapTo[Int] // as you are returning the multiplication of a*b
ans.foreach(println)
Note: CPU bound operations are not advised to use with actors it can have adverse effect on the performance of your application
As you want to receive a response from an actor you can use ask pattern for this purpose.
import akka.actor.Actor, ActorRef, ActorSystem, Props
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
case class CalsMul(a: Int, b: Int)
class Actor1 extends Actor
val r: ActorRef = context.actorOf(Props[Actor2], "master")
def receive: Receive =
case req: CalsMul =>
println("received message by Actor1")
r forward req
class Actor2 extends Actor
def receive: Receive =
case request: CalsMul =>
println("received message by Actor2")
Future.successful(request.a * request.b) pipeTo sender
object testObject extends App
implicit val system: ActorSystem = ActorSystem("ActorSystem")
val a: ActorRef = system.actorOf(Props[Actor1], "root")
implicit val timeout: Timeout = Timeout(20, SECONDS)
println(system, "sending message to Actor1")
val ans: Future[Int] = (a ? CalsMul(5, 15)).mapTo[Int] // as you are returning the multiplication of a*b
ans.foreach(println)
Note: CPU bound operations are not advised to use with actors it can have adverse effect on the performance of your application
edited Nov 15 '18 at 17:01
answered Nov 15 '18 at 16:33
Raman MishraRaman Mishra
1,4531418
1,4531418
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
add a comment |
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
Rather, if you have CPU-bound operations, don't try to complete them with a single message. Find some way to break the problem into pieces and either hand off the work to multiple children or send yourself messages triggering the sub-tasks. Letting children split the work can take advantage of more cores; telling yourself to work on it provides the equivalent of Thread.yield() calls, as the dispatcher has an opportunity to switch the thread to a different actor between steps.
– Rob Crawford
Nov 20 '18 at 17:17
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
No we should not do the CPU-bound operations inside the Future becuase it not meant to made for that rather we should use Futures for I/O bond operation because these are blocking calls and can run saparately on a thread no matter how many small chunks we give to the child actor cpu bound operation in Future is not a good practices. @RobCrawford
– Raman Mishra
Nov 21 '18 at 9:43
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
I did not say to use a Future. I have no idea where you got the idea I did.
– Rob Crawford
Nov 21 '18 at 14:58
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
Ask call always return a Future, you said that don't try to complete CPU-bound operations in a single message(which is an ask message) so i got the idea of Future. @RobCrawford
– Raman Mishra
Nov 22 '18 at 5:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323500%2fis-there-a-way-to-send-values-from-actor-to-actorref-in-actorsystem%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…
– Raman Mishra
Nov 15 '18 at 16:12