【Akka】Akka入门编程实例

引言

这篇文章主要是第一次学习Akka编程,先试试水,探探坑,对Akka和SBT的使用有一个直观的了解,以几个简单的akka编程实例来说明akka的使用。希望在日后的学习和编程中,能有更多自己的体会和经验总结来分享。

Actor模型

Actor实例可以想象成是服务器上的Web服务,你无法控制,只能通过发送消息去请求执行任务或查询信息,而不能直接在Web服务中修改状态或者处理资源。通过发送不可改变的消息,虽然看上去有些限制,但是可以很简单安全的编写并发程序。

Actor系统的形象理解

一个actor是基于Actor系统的最小单元,就像面向对象系统中的对象实例一样,它也封装了状态和行为。我们无法窥探actor内部的信息,只能通过发送消息来请求状态信息(就像是问一个人,他感觉如何)。actor中有一个存放不可变状态信息的信箱。我们通过发送信息和actor进行通信,当actor收到信息之后,它会运用相关算法来处理具体的信息。
在一个应用程序中,多个actor构成了一套层级系统,像是一个家族或者一个商业组织。一个actor可以认为是一个商业组织的个人。一个actor有一个父亲,称为监督者(supervisor),还有好多孩子,可以认为,在一个商业组织中,主席(actor)下面有多个副主席,副主席也有很多下属随从。
Actor系统的最佳实践是“委派任务”,尤其是当actor的行为被阻塞的时候。可以想象,在实际商业活动中,主席将要做的工作分配给下面的几个副主席去分别执行,而副主席也会将子任务分配给自己的随从,直到该任务被下属们执行完毕。

处理故障

Actor模型的一个重要内容是处理故障。在工作工程中,如果出现错误或者抛出异常,actor和其子actor都将暂停,然后发送一条信息给监督者(supervisor)actor,报告出现故障的信号。
根据工作任务和故障的性质,监督者actor将会作出几种选择:

  • 恢复下属actor,保留内部状态
  • 重启下属actor,清空状态
  • 终止下属actor
  • 上报故障

Hello,Actor实例

现在我用一个最简单的actor编程实例来介绍akka编程,先给出代码:

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props

class HelloActor extends Actor{
   
  def receive = {
    case "hello"  => println("hello back to you.")
    case _        => println("huh?")
  }
}

object Test1_HelloActor extends App {
   
  // actor need an ActorSystem
  val system = ActorSystem("HelloSystem")
  // create and start the actor
  val helloActor = system.actorOf(Props[HelloActor], name="helloActor")
  // send two messages
  helloActor ! "hello"
  helloActor ! "what"
  // shutdown the actor system
  system.shutdown
}

代码注解:

  • Actor由HelloActor定义
  • HelloActor的行为有receive方法定义实现,其中使用了模式匹配表达式
  • HelloActor接收字符串hello作为消息,做出相应打印动作
  • Test1_HelloActor的object用来测试actor
  • ActorSystem接收一个name参数,并且通过system.actorOf创建actor实例
  • 创建Actor实例名为helloActor,其构造函数没有参数
  • Actor创建后自动运行,不需调用start或者run方法
  • 通过!方法来发送消息

ActorSystem

一个actor system是actors的层级集团,分享公共配置信息(比如分发器dispatchers,部署deployments,远程功能remote capabilities,地址addresses)。它同时也是创建和查询actors的入口。ActorSystem是为你的应用程序分配线程资源的结构。

ActorRef

当你调用ActorSystemactorOf方法时,将创建并返回一个ActorRef的实例:
def actorOf(props: Props, name: String): ActorRef

这个引用用来处理actor,

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我能够为您提供一个简单的 Akka 应用实例。请注意,由于我是一个AI语言模型,所以我无法判断您是否已经熟悉了 Akka 框架的基本知识。如果您对 Akka 框架不熟悉,我建议您先去学习一下 Akka 的基础知识。 下面是一个使用 Java 编写的简单的 Akka 应用实例,其中定义了一个 Actor 类,它会接收来自其他 Actor 实例的消息并根据消息内容发送不同的响应消息: ``` import akka.actor.AbstractActor; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; public class AkkaExample { public static void main(String[] args) { // 创建 Actor 系统 ActorSystem system = ActorSystem.create("MySystem"); // 创建一个 Actor ActorRef myActor = system.actorOf(MyActor.props(), "myActor"); // 发送消息给 myActor myActor.tell("hello", ActorRef.noSender()); myActor.tell("world", ActorRef.noSender()); // 关闭 Actor 系统 system.terminate(); } public static class MyActor extends AbstractActor { public static Props props() { return Props.create(MyActor.class); } @Override public Receive createReceive() { return receiveBuilder() .match(String.class, msg -> { if ("hello".equals(msg)) { System.out.println("hello world"); } else { System.out.println("unknown message"); } }) .build(); } } } ``` 在这个例子中,我们首先创建了一个 Actor 系统,然后创建了一个名为 `myActor` 的 Actor 实例,并向该实例发送了两个消息。接着,在 Actor 实例定义中,我们使用 `receiveBuilder()` 方法创建了一个接收器,并使用 `match()` 方法将消息内容与处理程序关联起来。最后,我们在处理程序中根据不同的消息内容发送了不同的响应消息。 希望这可以帮助您了解如何使用 Akka 框架创建一个简单的应用实例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值