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

**URL:** https://forum.ficusonline.com/t/topic/386
**Category:** Programming
**Created:** [2020 年 10 月 11 日午後 2:59 UTC](https://forum.ficusonline.com/t/topic/386 "2020-10-11T14:59:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 10 月 11 日午後 2:59 UTC](https://forum.ficusonline.com/t/topic/386/1 "2020-10-11T14:59:20Z")

</div>

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

**Akka for JVM**

> **[Enterprise Agentic AI](https://akka.io/)**
>
> Build, run, and evaluate agentic AI systems with certainty, scale, and resilience. Get to production, quickly. Stay there, safely. Scale, cost effectively.

* * *

**Akkaドキュメント**

> **[Welcome to Akka :: Akka Documentation](https://doc.akka.io/)**

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

 ![akka-docs](https://forum.ficusonline.com/uploads/default/original/1X/e7e78305d983668928368e957fc67c807c9ee431.png)

* * *

**Getting Started**

> **[Example projects • Akka core](https://doc.akka.io/libraries/akka-core/current/project/examples.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

 ![akka_howtoguide](https://forum.ficusonline.com/uploads/default/original/1X/dd1f5637160dc6d083ca7112b96c0ee9cdacdb89.png)

* * *

**Akkaドキュメント**

> **[Getting Started Guide • Akka core](https://doc.akka.io/libraries/akka-core/current/typed/guide/index.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

**Akkaライブラリ**

> **[Akka 2.10.11 - akka](https://doc.akka.io/api/akka-core/current//akka/index.html)**
>
> Akka 2.10.11 - akka

**Akka Github**  
[https://github.com/akka/akka](https://github.com/akka/akka)

**MVNRepository**  
[https://mvnrepository.com/search?q=akka](https://mvnrepository.com/search?q=akka)

**Akkaサンプルコード＋ドキュメント**

> **[Example projects • Akka core](https://doc.akka.io/libraries/akka-core/current/project/examples.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 10 月 22 日午前 11:13 UTC](https://forum.ficusonline.com/t/topic/386/2 "2020-10-22T11:13:50Z")

</div>

> **[Actor References, Paths and Addresses • Akka core](https://doc.akka.io/libraries/akka-core/current/general/addressing.html)**
>
> Local and remote Akka Actor references, locating Actors, Actor paths and addresses.

 ![actor-paths-overview](https://forum.ficusonline.com/uploads/default/original/1X/a273d1d4ff9563277e9a03c376e8f731375e10eb.png)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 10 月 28 日午前 7:24 UTC](https://forum.ficusonline.com/t/topic/386/3 "2020-10-28T07:24:48Z")

</div>

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

> **[Learning Akka Typed from Classic • Akka core](https://doc.akka.io/libraries/akka-core/current/typed/from-classic.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

* * *

**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について**

> **[Articles](https://manuel.bernhardt.io/articles/#akka-typed)**
>
> This is an overview of some of what I’ve written over the years.
> Featured 10000 nodes and beyond with Akka Cluster and Rapid On pinning and and isolating CPU cores Akka Raspberry PI demos A new adaptive accrual failure detector for Akka SRE / DevOps...

* * *

## actorOf, ActorContext, ActorSystem, Props, ActorRef

> **[Learning Akka Typed from Classic • Akka core](https://doc.akka.io/libraries/akka-core/current/typed/from-classic.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

* * *

## actorOf

**ActorContext** または **ActorSystem** のメソッド

* * *

## ActorContext

**akka.actor.ActorContext**

> **[Akka 2.10.17 - akka.actor.ActorContext](https://doc.akka.io/api/akka-core/current//akka/actor/ActorContext.html)**
>
> Akka 2.10.17 - akka.actor.ActorContext

**akka.actor.typed.scaladsl.ActorContext**

> **[Akka 2.10.17 - akka.actor.typed.scaladsl.ActorContext](https://doc.akka.io/api/akka-core/current//akka/actor/typed/scaladsl/ActorContext.html)**
>
> Akka 2.10.17 - akka.actor.typed.scaladsl.ActorContext

* * *

## ActorSystem

**akka.actor.ActorSystem**

> **[Akka 2.10.17 - akka.actor.ActorSystem](https://doc.akka.io/api/akka-core/current//akka/actor/ActorSystem.html)**
>
> Akka 2.10.17 - akka.actor.ActorSystem

**akka.actor.typed.ActorSystem**

> **[Akka 2.10.17 - akka.actor.typed.ActorSystem](https://doc.akka.io/api/akka-core/current//akka/actor/typed/ActorSystem.html)**
>
> Akka 2.10.17 - akka.actor.typed.ActorSystem

* * *

## Props

**akka.actor.Props**

> **[Akka 2.10.17 - akka.actor.Props](https://doc.akka.io/api/akka-core/current//akka/actor/Props.html)**
>
> Akka 2.10.17 - akka.actor.Props

**akka.actor.typed.Props**

> **[Akka 2.10.17 - akka.actor.typed.Props](https://doc.akka.io/api/akka-core/current//akka/actor/typed/Props.html)**
>
> Akka 2.10.17 - akka.actor.typed.Props

* * *

## ActorRef

**akka.actor.ActorRef**

> **[Akka 2.10.17 - akka.actor.ActorRef](https://doc.akka.io/api/akka-core/current//akka/actor/ActorRef.html)**
>
> Akka 2.10.17 - akka.actor.ActorRef

**akka.actor.typed.ActorRef**

> **[Akka 2.10.17 - akka.actor.typed.ActorRef](https://doc.akka.io/api/akka-core/current//akka/actor/typed/ActorRef.html)**
>
> Akka 2.10.17 - akka.actor.typed.ActorRef

* * *

## Behavior(s)

### akka.actor.typed.Behavior

> **[Akka 2.10.17 - akka.actor.typed.Behavior](https://doc.akka.io/api/akka-core/current//akka/actor/typed/Behavior.html)**
>
> Akka 2.10.17 - akka.actor.typed.Behavior

### akka.actor.typed.scaladsl.Behaviors

> **[Akka 2.10.17 - akka.actor.typed.scaladsl.Behaviors](https://doc.akka.io/api/akka-core/current//akka/actor/typed/scaladsl/Behaviors$.html)**
>
> Akka 2.10.17 - akka.actor.typed.scaladsl.Behaviors

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 11 月 17 日午前 7:31 UTC](https://forum.ficusonline.com/t/topic/386/4 "2020-11-17T07:31:00Z")

</div>

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

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

> **[How the Actor Model Meets the Needs of Modern, Distributed Systems • Akka core](https://doc.akka.io/libraries/akka-core/current/typed/guide/actors-intro.html)**
>
> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

 ![serialized_timeline_invariants](https://forum.ficusonline.com/uploads/default/original/1X/6e3b961bcda8312b09e26620238321d71dc2956a.png)

- 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).

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 11 月 17 日午前 8:27 UTC](https://forum.ficusonline.com/t/topic/386/5 "2020-11-17T08:27:27Z")

</div>

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

> **[Introduction to Actors • Akka core](https://doc.akka.io/libraries/akka/snapshot/typed/actors.html)**
>
> The Actor model, managing internal state and changing behavior in Akka Actors.

> **[Introduction to Actors • Akka core](https://doc.akka.io/libraries/akka-core/current/typed/actors.html)**
>
> The Actor model, managing internal state and changing behavior in Akka Actors.

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

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

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

 ![hello-world2](https://forum.ficusonline.com/uploads/default/original/1X/a6a580551cdf2a2fa576b157b26fc22bc2c893f2.png)

```java
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)

```

**アウトプット**

```auto
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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 11 月 28 日午後 12:34 UTC](https://forum.ficusonline.com/t/topic/386/6 "2020-11-28T12:34:59Z")

</div>

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

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

**Configuration library for JVM**  
[https://github.com/lightbend/config#features-of-hocon](https://github.com/lightbend/config#features-of-hocon)

**HOCON**

> <https://github.com/lightbend/config/blob/master/HOCON.md>
