有没有一种方法可以在ActorSystem中将值从actor发送到actorRef [英] Is there a way to send values from actor to actorRef in ActorSystem
问题描述
我有两个actor,编码如下。
class Actor1扩展Actor {
val r:ActorRef = actorOf (道具[Actor2],主)
def接收:Receive = {
case class Mul(a,b)=>
r! CalcMul(a,b)
案例类别MulReply(ans)=>
println( Multiply result: + ans)
//要向测试对象发送 ans值。
}
}
类Actor2扩展了Actor {
def接收:Receive = {
案例类CalcMul(a,b)=>
个发件人! MulReply(a * b)
}
}
对象testObject扩展了App {
val a = ActorSystem( ActorSystem)。actorOf(Props [Actor1], root)
a! CalcMul(5,15)
//如何在此处接收 ans值?
}
我能够在中接收并打印结果Actor1
,但是在 testObject
中需要这些值,因此我可以将它们用于将来的操作。无法在 testObject
中使用 receive
方法,以在 Actor1 $中接收消息c $ c>是来自
Actor2
的,因此不能使用 tell
方法发送。
如果您希望收到演员的回应,则可以使用Ask模式。
导入akka.actor。{演员,ActorRef,ActorSystem,道具}
$ p的乘法$ p>
导入akka.pattern._
导入akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.SECONDS
案例类CalsMul(a :Int,b:Int)
类Actor1扩展了Actor {
val r:ActorRef = context.actorOf(Props [Actor2], master)
def receive:Receive = {
case req:CalsMul =>
println( Actor1收到的消息)
r转发要求
}
}
类Actor2扩展了Actor {
def接收:接收= {{
例请求:CalsMul =>
println( Actor2收到的消息)
Future.successful(request.a * request.b)pipeTo发送者
}
}
对象testObject扩展了App {
隐式val系统:ActorSystem = ActorSystem( ActorSystem)
val a:ActorRef = system.actorOf(Props [Actor1], root)
隐式val超时:超时=超时(20,秒)
println(system,向Actor1发送消息)
值:Future [Int] =(a?CalsMul(5,15))。 mapTo [Int] //返回a * b
ans.foreach(println)
}
注意:不建议将CPU绑定操作与actor一起使用,否则可能会对应用程序的性能产生不利影响
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 intestObject
so I can use them for future operations. Cannot have areceive
method intestObject
as done to receive a message inActor1
fromActor2
, so cannot send them withtell
method.解决方案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
这篇关于有没有一种方法可以在ActorSystem中将值从actor发送到actorRef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!