Akka并发编程——第四节:Actor模型(三)

时间:2022-09-08 20:23:29

本将主要内容:
1. Actor引用、Actor路径

1. Actor引用、Actor路径

下图是Akka官方文档中给出的一张图

Akka并发编程——第四节:Actor模型(三)

该图清晰地说明了ActorPath,ActorRef,Actor及ActorSystem之间的关系,并说明了Actor整体的层次结构。前面我们提到,Akka应用程序会持有一个名称为user的Actor,该Actor被称为guardian supervisor(守卫监督器),无论是ActorSystem创建的Actor还是通过ActorContext创建的Actor都为user的子类,它是最*的Actor。

(一)ActorRef

对于ActorRef,我们已经很熟悉了,通过调用ActorSystem.actorOf方法可以创建Actor,返回的便是ActorRef,例如代码

 //创建FirstActor对象
val myactor = system.actorOf(Props[FirstActor], name = "firstActor")

返回的便是FirstActor的ActorRef对象,ActorRef最重要的作用便是向Actor发送消息,例如

//向myactor发送消息
myactor!"test"
myactor! 123

另外,还可以通过context隐式对象获取父Actor和子Actor的ActorRef,示例代码如下:

/*
*Actor API:成员变量self及sender()方法的使用
*/

object Example_07 extends App{
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

class FirstActor extends Actor with ActorLogging{
//通过context.actorOf方法创建Actor
var child:ActorRef = _
override def preStart(): Unit ={
log.info("preStart() in FirstActor")
//通过context上下文创建Actor
child = context.actorOf(Props[MyActor], name = "myActor")
}
def receive = {
//向MyActor发送消息
case x => child ! x;log.info("received "+x)
}
}

class MyActor extends Actor with ActorLogging{
var parentActorRef:ActorRef=_
override def preStart(): Unit ={
//通过context.parent获取其父Actor的ActorRef
parentActorRef=context.parent
}
def receive = {
case "test" => log.info("received test");parentActorRef!"message from ParentActorRef"
case _ => log.info("received unknown message");
}

}
val system = ActorSystem("MyActorSystem")
val systemLog=system.log

//创建FirstActor对象
val myactor = system.actorOf(Props[FirstActor], name = "firstActor")
//获取ActorPath
val myActorPath=system.child("firstActor")
//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(myActorPath)
systemLog.info("准备向myactor发送消息")
//向myActor1发送消息
myActor1!"test"
myActor1! 123
Thread.sleep(5000)
//关闭ActorSystem,停止程序的运行
system.shutdown()
}

代码运行结果

[INFO] [04/02/2016 20:28:08.941] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor] preStart() in FirstActor
[INFO] [04/02/2016 20:28:08.942] [main] [ActorSystem(MyActorSystem)] 准备向myactor发送消息
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-3] [akka://MyActorSystem/user/firstActor] received test
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-3] [akka://MyActorSystem/user/firstActor] received 123
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received test
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received unknown message
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-3] [akka://MyActorSystem/user/firstActor] received message from ParentActorRef
[INFO] [04/02/2016 20:28:08.943] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received unknown message

代码

 class MyActor extends Actor with ActorLogging{
var parentActorRef:ActorRef=_
override def preStart(): Unit ={
//通过context.parent获取其父Actor的ActorRef
parentActorRef=context.parent
}
def receive = {
case "test" => log.info("received test");parentActorRef!"message from ParentActorRef"
case _ => log.info("received unknown message");
}

}

中,使用

//通过context.parent获取其父Actor的ActorRef
parentActorRef=context.parent

获取MyActor 的直接父Actor的ActorRef,在本例中为FirstActor,因为在FirstActor中,我们使用

 //通过context上下文创建Actor
child = context.actorOf(Props[MyActor], name = "myActor")

创建了MyActor,FirstActor便自动成为MyActor的直接Supervisor。如此便可以通过代码

parentActorRef!"message from ParentActorRef"

发送消息。

另外,还可以通过

//创建FirstActor对象
val myactor = system.actorOf(Props[FirstActor], name = "firstActor")
//获取ActorPath
val myActorPath=system.child("firstActor")
//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(myActorPath)

system.actorSelection(myActorPath)方法获取相应的ActorRef。然后再使用获取到的ActorRef向Acto发送消息

 //向myActor1发送消息
myActor1!"test"
myActor1! 123

现在,让我们来总结一下获取ActorRef的方法:
(1)通过ActorSystem的actorOf方法,不过这种方式是通过创建Actor,然后返回其ActorRef
(2)通过context.actorOf方法,这种方式也是通过创建Actor,然后返回其ActorRef
(3)通过context.parent、context.self、context.children方法获取当前Actor的父Actor、当前Actor及子Actor的ActorRef,这种方式是获取已经创建好的Actor的ActorRef
(4)通过val myActor1=system.actorSelection(myActorPath)方法来获取ActorRef,这种方式也是获取已经创建好的Actor的ActorRef

(二)ActorPath

在前面的例子中,我们通过

 val myActorPath=system.child("firstActor")

已经使用到了ActorPath。在Akka中,ActorPath采用统一资源定位符的方式进行组织,例如

//本地ActorPath
"akka://my-sys/user/service-a/worker1"
//远程ActorPath
"akka.tcp://my-sys@host.example.com:5678/user/service-b"

本地ActorPath是Akka并发编程中的ActorPath表示方式,而远程ActorPath是Akka分布式编程中常用的ActorPath表示方式,”akka.tcp://my-sys@host.example.com:5678/user/service-b”中的TCP表示使用的是TCP协议,也可以使用UPD协议,使用UDP协议的远程ActorPath表示方式有如下形式

"akka.udp://my-sys@host.example.com:5678/user/service-b"

ActorPath当中,akka.udp表示的是使用的协议,my-sys表示的是ActorSytem名称,host.example.com:5678为远程机器地址及端口,user为guardian supervisor,service-b为当前应用程序的*Actor(通过system.actorOf方法创建的)

/*
*ActorPath
*/

object Example_08 extends App{
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

class FirstActor extends Actor with ActorLogging{
//通过context.actorOf方法创建Actor
var child:ActorRef = _
override def preStart(): Unit ={
log.info("preStart() in FirstActor")
//通过context上下文创建Actor
child = context.actorOf(Props[MyActor], name = "myActor")
}
def receive = {
//向MyActor发送消息
case x => child ! x;log.info("received "+x)
}
}

class MyActor extends Actor with ActorLogging{
def receive = {
case "test" => log.info("received test");
case _ => log.info("received unknown message");
}

}
val system = ActorSystem("MyActorSystem")
val systemLog=system.log

//创建FirstActor对象
val firstactor = system.actorOf(Props[FirstActor], name = "firstActor")

//获取ActorPath
val firstActorPath=system.child("firstActor")
systemLog.info("firstActorPath--->{}",firstActorPath)


//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(firstActorPath)

//直接指定其路径
val myActor2=system.actorSelection("/user/firstActor")
//使用相对路径
val myActor3=system.actorSelection("../firstActor")


systemLog.info("准备向myactor发送消息")
//向myActor1发送消息
myActor2!"test"
myActor2! 123
Thread.sleep(5000)
//关闭ActorSystem,停止程序的运行
system.shutdown()
}

代码运行结果:

[INFO] [04/02/2016 21:04:59.612] [main] [ActorSystem(MyActorSystem)] firstActorPath--->akka://MyActorSystem/user/firstActor
[INFO] [04/02/2016 21:04:59.612] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] preStart() in FirstActor
[INFO] [04/02/2016 21:04:59.615] [main] [ActorSystem(MyActorSystem)] 准备向myactor发送消息
[INFO] [04/02/2016 21:04:59.616] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor] received test
[INFO] [04/02/2016 21:04:59.616] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor] received 123
[INFO] [04/02/2016 21:04:59.616] [MyActorSystem-akka.actor.default-dispatcher-3] [akka://MyActorSystem/user/firstActor/myActor] received test
[INFO] [04/02/2016 21:04:59.616] [MyActorSystem-akka.actor.default-dispatcher-3] [akka://MyActorSystem/user/firstActor/myActor] received unknown message

本例中的重点代码如下

//获取ActorPath
val firstActorPath=system.child("firstActor")
systemLog.info("myActorPath--->{}",firstActorPath)


//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(firstActorPath)

//直接指定其路径
val myActor2=system.actorSelection("/user/firstActor")
//使用相对路径
val myActor3=system.actorSelection("../firstActor")

通过 val firstActorPath=system.child(“firstActor”)构造一个ActorPath,然后使用

val myActor1=system.actorSelection(firstActorPath)

获取路径下的ActorRef,除这种方式外,还可以通过绝对路径 val myActor2=system.actorSelection(“/user/firstActor”)及相对路径 val myActor3=system.actorSelection(“../firstActor”)的方式获取ActorRef
,需要注意的是绝对路径使用的是guardian supevisor,即/user/firstActor的这种方式。

如果要获取myActor,则基方法是类型的,例如

//获取ActorPath
val myActorPath=system.child("firstActor").child("myActor")
systemLog.info("firstActorPath--->{}",myActorPath)


//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(myActorPath)

//直接指定其路径
val myActor2=system.actorSelection("/user/firstActor/myActor")
//使用相对路径
val myActor3=system.actorSelection("../firstActor/myActor")

完整代码如下:

/*
*ActorPath,获取myActor
*/

object Example_09 extends App{
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

class FirstActor extends Actor with ActorLogging{
//通过context.actorOf方法创建Actor
var child:ActorRef = _
override def preStart(): Unit ={
log.info("preStart() in FirstActor")
//通过context上下文创建Actor
child = context.actorOf(Props[MyActor], name = "myActor")
}
def receive = {
//向MyActor发送消息
case x => child ! x;log.info("received "+x)
}
}

class MyActor extends Actor with ActorLogging{
def receive = {
case "test" => log.info("received test");
case _ => log.info("received unknown message");
}

}
val system = ActorSystem("MyActorSystem")
val systemLog=system.log

//创建FirstActor对象
val firstactor = system.actorOf(Props[FirstActor], name = "firstActor")

//获取ActorPath
val myActorPath=system.child("firstActor").child("myActor")
systemLog.info("firstActorPath--->{}",myActorPath)


//通过system.actorSelection方法获取ActorRef
val myActor1=system.actorSelection(myActorPath)

//直接指定其路径
val myActor2=system.actorSelection("/user/firstActor/myActor")
//使用相对路径
val myActor3=system.actorSelection("../firstActor/myActor")


systemLog.info("准备向myactor发送消息")
//向myActor1发送消息
myActor1!"test"
myActor1! 123
Thread.sleep(5000)
//关闭ActorSystem,停止程序的运行
system.shutdown()
}

代码运行结果:

[INFO] [04/02/2016 21:21:14.377] [main] [ActorSystem(MyActorSystem)] firstActorPath--->akka://MyActorSystem/user/firstActor/myActor
[INFO] [04/02/2016 21:21:14.377] [MyActorSystem-akka.actor.default-dispatcher-2] [akka://MyActorSystem/user/firstActor] preStart() in FirstActor
[INFO] [04/02/2016 21:21:14.381] [main] [ActorSystem(MyActorSystem)] 准备向myactor发送消息
[INFO] [04/02/2016 21:21:14.382] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received test
[INFO] [04/02/2016 21:21:14.382] [MyActorSystem-akka.actor.default-dispatcher-4] [akka://MyActorSystem/user/firstActor/myActor] received unknown message

关于远程ActorRef的获取,我们将在后续章节中进行讲述。



Scala学习(公众微信号:ScalaLearning)每天为大家带来一点Scala语言、Spark、Kafka、Flink、AKKA等大数据技术干货及相关技术资讯。技术永无止境,勇攀高峰,一往直前!
觉得文章不错?扫描关注
Akka并发编程——第四节:Actor模型(三)