Is there a way to send values from actor to actorRef in ActorSystem










0















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.










share|improve this question






















  • yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…

    – Raman Mishra
    Nov 15 '18 at 16:12
















0















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.










share|improve this question






















  • yes you can do it using ask look at this: doc.akka.io/docs/akka/current/…

    – Raman Mishra
    Nov 15 '18 at 16:12














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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













1 Answer
1






active

oldest

votes


















1














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






share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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






share|improve this answer

























  • 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















1














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






share|improve this answer

























  • 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













1












1








1







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands