KZKY memo

自分用メモ.

怒涛のAkka: Mailboxes

1 acotr: 1mailboxだけれども,BalancingPoolのようにそうでない場合もある.

message queueのタイプをアクターに要求

RequiresMessageQueueをタイプ付きでインプリ

import akka.dispatch.RequiresMessageQueue
import akka.dispatch.BoundedMessageQueueSemantics
class MyBoundedActor extends MyActor
    with RequiresMessageQueue[BoundedMessageQueueSemantics]

Configurationで設定

bounded-mailbox {
    mailbox-type = "akka.dispatch.BoundedMailbox"
    mailbox-capacity = 1000
    mailbox-push-timeout-time = 10s
}
akka.actor.mailbox.requirements {
    "akka.dispatch.BoundedMessageQueueSemantics" = bounded-mailbox
}

dispatcherの設定,ハードコードでmailboxが指定されると上書きされるので注意.

Requiring a Message Queue Type for a Dispatcher

Dispatcherから要求する場合のConfig

my-dispatcher {
    mailbox-requirement = org.example.MyInterface
}

Mailbox typeの決定順序

次の順に決定される.先勝ちなのか?

  • コンフィグ上,actorのdeploymentにmailbox keyがある
  • withMailboxでのハードコード
  • コンフィグ上,dispatcherのmailbox-type keyがある
  • 上記のように,actorがmailbox typeを要求する場合
  • 上記のように,dispatcherがmailbox typeを要求する場合
  • defaultのmailbox type

Default mailbox

mailboxのデフォルト

  • java.util.concurrent.ConcurrentLinkedQueue
  • unbounded

SingleConsumerOnlyUnboundedMailboxもいいらしい.

設定はこれで可能

akka.actor.default-mailbox {
    mailbox-type = "akka.dispatch.SingleConsumerOnlyUnboundedMailbox"
}

Mailbox Typeに渡すConfig

ActorSystem.SettingsとConfig
カスタムMailboxの作成時に必要.

Built-in mailbox

いっぱいある

  • UnboundedMailbox
  • SingleConsumerOnlyUnboundedMailbox
  • BoundedMailbox
  • NonBlockingBoundedMailbox
  • UnboundedPriorityMailbox
  • BoundedPriorityMailbox
  • UnboundedControlAwareMailbox
  • BoundedControlAwareMailbox

基本名前の通り.詳細はここのBuiltin implementationsを参考にする.
http://doc.akka.io/docs/akka/2.3.9/scala/mailboxes.html

Mailbox Config サンプル

Priority付きのMailboxの例

UnboundedPriorityMailboxをextendsしてPriorityGeneratorをoverrideする.
そして,mailboxを指定する.指定の仕方はハードコードやconfigに書くといろいろ可能.

package edu.kzk.actor.mailbox

import akka.dispatch.PriorityGenerator
import akka.dispatch.UnboundedPriorityMailbox
import com.typesafe.config.Config
import akka.actor.ActorSystem
import akka.actor.PoisonPill

// We inherit, in this case, from UnboundedPriorityMailbox
// and seed it with the priority generator
class MyPrioMailbox(settings: ActorSystem.Settings, config: Config)
    extends UnboundedPriorityMailbox(
      // Create a new PriorityGenerator, lower prio means more important
      PriorityGenerator {
        // ’highpriority messages should be treated first if possible
        case 'highpriority => 0
        // ’lowpriority messages should be treated last if possible
        case 'lowpriority => 2
        // PoisonPill when no other left

        case PoisonPill => 3
        // We default to 1, which is in between high and low
        case otherwise => 1
      }) {

}
  • ActorWithPriorityMailbox.scala
package edu.kzk.actor.mailbox

import akka.actor.Actor
import akka.event.LoggingAdapter
import akka.event.Logging

class ActorWithPriorityMailbox extends Actor {
  val log: LoggingAdapter = Logging(context.system, this);

  def receive = {
    case x => log.info(x.toString)
  }
}
  • reference.conf
prio-mailbox {
	mailbox-type = "edu.kzk.actor.mailbox.MyPrioMailbox"
	//Other mailbox configuration goes here
}

refernece.confはbuild pathのrootに置けば良いと思う.
eclipseの場合は/path/to/project/src/mani/resourcesで良い(buildpathは通すこと)

ここによると,
akka applicationを書くなら,class-pathのrootにapplication.confをおく
akka-based libraryを書くなら,class-pathのrootにreference.confをおく

  • PriorityMailboxApp.scala
package edu.kzk.actor.mailbox

import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.PoisonPill

object PriorityMailboxApp extends App {

  implicit val system = ActorSystem("priority-mailbox-system");
  val actor = system.actorOf(Props[ActorWithPriorityMailbox].withMailbox("prio-mailbox"));

  actor ! 'lowpriority
  actor ! 'lowpriority
  actor ! 'highpriority
  actor ! 'pigdog
  actor ! 'pigdog2
  actor ! 'pigdog3
  actor ! 'highpriority
  actor ! PoisonPill

  system.shutdown()
}

ControlAwareMailbox

どんなメッセージがいくつもmailboxにあってもcontroll massageはすぐに処理する.
akka.dispatch.ControlMessageがないってEclipseに怒られるのでペンディング

カスタムMailBoxを作る

サンプルでやっていることの全体像

メールボックスのobject/classを作る

  • objectの中
    • queue class
      • MessageQueueをextentds
      • XxxSemanticsをwith
      • XxxSemanticsはrequirementのマーカーになるので定義しておく
      • queueに対する操作を定義
  • classの中
    • MailboxTypeをextends
    • ProducesMessageQueue[queue class]をwith
    • this(ActorSystem.Settings, Config)を定義
    • create: MessageQueueでqueue classをnewする

詳しくはここのCreating your own Mailbox typeを参考.