Akka TypedActor

Akka 中的有类型 Actor 是 Active Objects 模式的一种实现,将异步的调用执行逻辑封装在一个方法内,在代码层面保证了的顺序执行思维。

Active Objects 设计模式

来自维基百科 Active Objects

该设计模式包含了六种元素:

  • 代理:提供了面向客户端的带有公开方法的接口。
  • 接口:定义了到 active object 的请求方法(业务代码提供)。
  • 来自客户端的一序列等待请求。
  • 调度器:决定接下来执行哪个请求。
  • active object 方法的实现类(业务代码提供)。
  • 一个回调或变量,用以让客户端接收结果。

上述六个元素中,除了标记(业务代码提供)的,其余都是由该模式的实现提供的,在本篇也就是 Akka。

Akka 是通过 JDK 的 java.lang.reflect.Proxy 来自实现 active object 模式的。

使用 TypedActor

TypedActor 将消息传递隐藏在方法调用内,但它也必须满足 Actor 顺序地、独占地执行 Actor 代码逻辑的要求。

有类型Actor相对于普通Actor的优势在于有类型Actor拥有静态的契约,你不需要定义你自己的消息,它的劣势在于对你能做什么和不能做什么进行了一些限制,i.e. 你不能使用 become/unbecome。

1. 定义业务接口和实现

trait Square {
    // 周长
    def circumference: Long

    // 面积
    def area: Long
}

class SquareImpl(val length: Long, val width: Long) extends Square {

    def circumference = {
        println("get circumference request")
        (length + width) * 2
    }

    def area = {
        println("wait before area")
        Thread.sleep(2000)
        length * width
    }
}

2. 封装成 active object 非常简单

val square = TypedActor(system).typedActorOf(TypedProps(classOf[Square], new SquareImpl(10, 20)), "name")

3. 测试 TypedActor 的执行效果

class SenderActor(val square: Square) extends Actor {
    def receive = {
        case "area" => println(square.area)
        case "circumference" => println(square.circumference)
    }
}

object LearnTypedActor {
    def main(vars: Array[String]) {
        val system = ActorSystem("myActorSystem")
        val square = TypedActor(system).typedActorOf(TypedProps(classOf[Square], new SquareImpl(10, 20)), "name")

        val actor1 = system.actorOf(Props(new SenderActor(square)), "send1")
        actor1 ! "area"
        println("message sent to actor 1")

        val actor2 = system.actorOf(Props(new SenderActor(square)), "send2")
        actor2 ! "circumference"
        println("message sent to actor 2")
    }
}

这里定义了一个 SenderActor 是为了并发地调用 TypedActor 实现的方法。由于 area 方法的实现里进行了等待,circumference 没有,如果 TypedActor 不是顺序地处理消息,那么 circumference 应该比 area 先执行,否则是后执行。

4. 结果

message sent to actor 1
wait before area
message sent to actor 2
200
get circumference request
60

结果是符合预期的,先调用的 area 方法虽然进行了等待,但后调用的 circumference 还是得等 area 方法执行完后才能执行。

TypedActor 的优势

如果我们通过消息传递的方式来调用,就需要编写代码的人具有异步的思维,异步地思考,而人的思维模式都是顺序地思考的,这就需要很大的转变。

TypedActor 保持了消息传递的优点,且 Akka 框架屏蔽了异步性,允许程序员继续用顺序的思维思考问题。

另一个优势就是它是有类型的,由于消息传递被封装在方法调用内,所以传递的消息都有了类型安全的保障,直接使用 Actor,可能会收到任意类型的消息。


欢迎关注我的微信公众号: coderbee笔记,可以更及时回复你的讨论。

Akka TypedActor》有一个想法

回复 NathanJiang 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据