Scala(Java)のアクターモデルライブラリ(ツールキット)Akka

Akkaとは、ScalaJavaアクターモデルを採用したプログラムを構築する上で必要となるツールキット(ライブラリ)です。
ヒエラルキー構造で各々が役割(プログラム)を持ったアクター群から構成されるアクターモデルにより、ヒエラルキー上層に位置するアクター(システムアクター、ガーディアンアクター)が、必要に応じて新規アクターを作成または削除したり、役割に応じて各アクターにジョブをメッセージにより振分けます。アクターモデルは、アクターにより処理が細かく分割できることと、アクター相互のコミュニケーションがメッセージのみという簡易な手段のため、非同期処理・拡張性・エラー耐性が必要なプログラムに向いたアルゴリズムです。

Akka for JVM


Akkaドキュメント

Akka Actors, Akka Streams, Akka HTTP 等モジュール別のリファレンスです。


Getting Started
https://developer.lightbend.com/guides/


Akkaドキュメント

Akkaライブラリ

Akka Github

MVNRepository
https://mvnrepository.com/search?q=akka

Akkaサンプルコード+ドキュメント
https://developer.lightbend.com/start/?group=akka

改訂ライブラリAkka Typedと旧ライブラリAkka Classicの比較


Classicに対するTypedの該当名

Classic Typed
akka-actor akka-actor-typed
akka-cluster akka-cluster-typed
akka-cluster-sharding akka-cluster-sharding-typed
akka-cluster-tools akka-cluster-typed
akka-distributed-data akka-cluster-typed
akka-persistence akka-persistence-typed
akka-stream akka-stream-typed
akka-testkit akka-actor-testkit-typed

パッケージ名称

Classic Typed for Scala Typed for Java
akka.actor akka.actor.typed.scaladsl akka.actor.typed.javadsl
akka.cluster akka.cluster.typed akka.cluster.typed
akka.cluster.sharding akka.cluster.sharding.typed.scaladsl akka.cluster.sharding.typed.javadsl
akka.persistence akka.persistence.typed.scaladsl akka.persistence.typed.javadsl

Akka Typedについて


actorOf, ActorContext, ActorSystem, Props, ActorRef


actorOf

ActorContext または ActorSystem のメソッド


ActorContext

akka.actor.ActorContext

akka.actor.typed.scaladsl.ActorContext


ActorSystem

akka.actor.ActorSystem

akka.actor.typed.ActorSystem


Props

akka.actor.Props

akka.actor.typed.Props


ActorRef

akka.actor.ActorRef

akka.actor.typed.ActorRef


Behavior(s)

akka.actor.typed.Behavior

akka.actor.typed.scaladsl.Behaviors

アクターモデルコンセプト

各アクターには、他のアクターからのメッセージを受信するためのアドレスメールボックスが割当てられており、メッセージの内容に従い各々のアクターがタスクを実行します。behaviorはアクターのタスクの進行状況などを表します。メッセージを送受信した場合などステータスが変化します。

serialized_timeline_invariants

  • A mailbox (the queue where messages end up).
  • A behavior (the state of the actor, internal variables etc.).
  • Messages (pieces of data representing a signal, similar to method calls and their parameters).
  • An execution environment (the machinery that takes actors that have messages to react to and invokes their message handling code).
  • An address (more on this later).

Akkaアクターモデルサンプルコード

以下の3つのメッセージが各アクターにより使用されます(コード参照)。

Greet: Greeterアクターへの挨拶の送信メッセージ
Greeted: Greeterアクターからの挨拶されたことへの返信メッセージ
SayHello: GreeterMainへの挨拶のプロセスを開始するメッセージ

HelloWorldMain: 管理者アクター(アクターシステムの要、アプリの起点)
HelloWorld, HelloWorldBot: 管理者アクターから産出されるアクター

hello-world2

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.scaladsl.LoggerOps
import akka.actor.typed.{ ActorRef, ActorSystem, Behavior }

object HelloWorld {
  final case class Greet(whom: String, replyTo: ActorRef[Greeted])
  final case class Greeted(whom: String, from: ActorRef[Greet])

  def apply(): Behavior[Greet] = Behaviors.receive { (context, message) =>
    println(s"Hello ${message.whom}!")
    message.replyTo ! Greeted(message.whom, context.self)
    Behaviors.same
  }
}

object HelloWorldBot {

  def apply(max: Int): Behavior[HelloWorld.Greeted] = {
    bot(0, max)
  }

  private def bot(greetingCounter: Int, max: Int): Behavior[HelloWorld.Greeted] =
    Behaviors.receive { (context, message) =>
      val n = greetingCounter + 1
      println(s"Greeting $n for ${message.whom}")
      if (n == max) {
        Behaviors.stopped
      } else {
        message.from ! HelloWorld.Greet(message.whom, context.self)
        bot(n, max)
      }
    }
}

object HelloWorldMain {

  final case class SayHello(name: String)

  def apply(): Behavior[SayHello] =
    Behaviors.setup { context =>
      val greeter = context.spawn(HelloWorld(), "greeter")

      Behaviors.receiveMessage { message =>
        val replyTo = context.spawn(HelloWorldBot(max = 3), message.name)
        greeter ! HelloWorld.Greet(message.name, replyTo)
        Behaviors.same
      }
    }

  def main(args: Array[String]): Unit = {
    val system: ActorSystem[HelloWorldMain.SayHello] =
      ActorSystem(HelloWorldMain(), "hello")

    system ! HelloWorldMain.SayHello("World")
    system ! HelloWorldMain.SayHello("Akka")
  }
}

// This is run by ScalaFiddle
HelloWorldMain.main(Array.empty)

アウトプット

Hello World!
Hello Akka!
Greeting 1 for World
Hello World!
Greeting 1 for Akka
Hello Akka!
Greeting 2 for World
Greeting 2 for Akka
Hello World!
Hello Akka!
Greeting 3 for World
Greeting 3 for Akka

アプリの設定ファイルはHOCONフォーマットJSONフォーマットを、より簡略化して見易くしたもの)で記述。

application.conf —This file should contain the configuration properties in the
HOCON format.

Configuration library for JVM

HOCON