Akka Actor銇瓻xtension銈掑皯銇楄銇广仸銇裤仧


銇撱倱銇仭銇@s_tsuka銇с仚銆

浠婂洖銇疉kka Actor銇瓻xtension銇仱銇勩仸灏戙仐瑾裤伖銇熴倞瑭︺仐銇熴倞銇椼仸銇裤仧銇仹銆併仢銇鏌荤祼鏋溿倰鏇搞亜銇︺亜銇嶃伨銇欍

Actor銇瓻xtension銇ㄣ伅

鍏紡銉夈偔銉ャ儭銉炽儓銇互涓嬨仹銇欍

http://doc.akka.io/docs/akka/2.4.0/scala/extending-akka.html

ActorSystem銇斻仺銇紤搴︺儹銉笺儔銇曘倢銈婣ctor銇嫛寮垫鑳姐倰浣溿倢銈嬫鑳姐亴Actor Extensions銇ㄣ亜銇嗐亾銇ㄣ倝銇椼亜銇с仚銆

姝g洿銈堛亸銈忋亱銈夈仾銇勩伄銇с併伨銇氥伅銈点兂銉椼儷銈炽兗銉夈倰鏇搞亜銇︺伩銇俱仚銆

Extension銇偟銉炽儣銉

涓婅銇叕寮忋伀銈点兂銉椼儷銇屻亗銈嬨伄銇с併亾銈屻倰浣裤仯銇︾啊鍗樸仾銉椼儹銈般儵銉犮倰鏇搞亜銇︺伩銇俱仚銆

銇俱仛銇痓uild.sbt銇

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.4.0"
)

銈掕拷鍔犮仐銇︺併仢銇緦Main.scala銈掓浉銇嶃伨銇欍

import java.util.concurrent.atomic.AtomicLong

import akka.actor._

object Main {
  def main(args: Array[String]): Unit = {
    val system1 = ActorSystem("my-system")
    val system2 = ActorSystem("my-system2")

    val actor1 = system1.actorOf(Props(classOf[MyActor]))
    val actor2 = system2.actorOf(Props(classOf[MyActor]))

    CountExtension(system1).increment() // a : 0 => 1
    actor1 ! "msg" // a : 1 => 2
    actor1 ! "msg" // a : 2 => 3
    actor1 ! "msg" // a : 3 => 4

    Thread.sleep(1000)
    actor2 ! "msg" // b : 0 => 1

    system1.terminate()
    system2.terminate()
  }
}

class MyActor extends Actor {
  val counter = CountExtension(context.system)

  def receive = {
    case someMessage =>
      val result = counter.increment()
      println(s"result = $result")
  }
}

class CountExtensionImpl extends Extension {
  private val counter = new AtomicLong(0)

  def increment() = counter.incrementAndGet()
}

object CountExtension
  extends ExtensionId[CountExtensionImpl]
  with ExtensionIdProvider {

  override def lookup = CountExtension

  override def createExtension(system: ExtendedActorSystem) = new CountExtensionImpl

  override def get(system: ActorSystem): CountExtensionImpl = super.get(system)
}

銇撱倢銈掑嫊銇嬨仚銇ㄤ互涓嬨伄銈堛亞銇嚭鍔涖仌銈屻倠銇嬨仺鎬濄亜銇俱仚銆

result = 2
result = 3
result = 4
result = 1

Extension銇倛銇c仸銉°儍銈汇兗銈搞倰鍙椼亼鍙栥倠銇熴伋銇偒銈︺兂銈裤倰澧楀姞銇椼仸銇痯rintln銇欍倠銉汇兓銇ㄣ亜銇嗐仩銇戙伄鐗广伀鎰忓懗銇仾銇勩儣銉偘銉┿儬銇с仚銆

浣曞洖銇嬧漨sg鈥濄倰閫併仯銇︺亜銈嬨伄銇с仢銇害鏁板瓧銇屽銇堛仸銇勩伨銇欍

銈点兂銉椼儷銇В瑾

CountExtension銇疉ctorSystem銇仧銈併伄銇婃焙銇俱倞銇偝銉笺儔銆併伩銇熴亜銇倐銇仹銇欍侰ountExtension銇痑pply銈掓寔銇c仸銇勩仸銆丄ctorSystem銈掗銈忋仜銈嬨仺CountExtensionImpl銈掑彇寰椼仹銇嶃伨銇欍

銇撱伄閮ㄥ垎銇с仚銇

CountExtension(context.system)

鎷″嫉鏈綋銇疌ountExtensionImpl銇с併亾銈屻伅鍗樸伀銈偊銉炽偪銈掑姞绠椼仐銇︺亜銈嬨仩銇戙仹銇欍

鎷″嫉銇疉ctorSystem銇斻仺銇紤搴︺仩銇戙儹銉笺儔銇曘倢銈嬨伄銇с佷笂瑷樸伄銈炽兗銉夈仹銇痵ystem1銇╯ystem2銇с仢銈屻仦銈屽垾銇嫛寮(object)銈掓寔銇c仸銇勩伨銇欍傘仾銇仹銆佸嚭鍔涖亴result 2, 3, 4銇ㄦ潵銇1銇ㄣ仾銇c仸銇勩倠瑷炽仹銇欍伃銆

ActorSystem銇斻仺銇紤搴︺仩銇戙儹銉笺儔銇ㄨ█銇c仸銇勩伨銇欍亴銆丆ountExtension銇垵鏈熷寲銈扐ctorSystem浣滄垚鏅傘伀琛屻亞銇伅銉夈偔銉ャ儭銉炽儓銇緭銇c仸浠ヤ笅銇倛銇嗐伀config銈掕拷鍔犮仐銇俱仚銆

akka {
  extensions = ["docs.extension.CountExtension"]
}

docs.extension.CountExtension銇儴鍒嗐伅閬╁疁銆併儜銉冦偙銉笺偢鍚嶏紜銈儵銈瑰悕銇鏇淬仐銇︺亸銇犮仌銇勩

config銇仐銇牬鍚堛伅Extension銇產pply(ActorSystem)銇屽疅琛屻仌銈屻仧銇ㄣ亶銇垵鏈熷寲銇曘倢銈嬨伅銇氥仹銇欍

銇亰銆丒xtension銈掍娇銇嗐仺銈广儸銉冦儔銈汇兗銉曘仹銇亜銇撱仺銇仾銈嬨仺鎬濄亜銇俱仚銆傝┏绱般伅銇撱仭銈夈倰銇旇Η銇忋仩銇曘亜銆

Actor Extension銇偣銉儍銉夈偄銉炽偦銉笺儠銇偣銇敞鎰

Extension銈掍娇銇嗐仺銇嶃伅鍚堛倧銇涖仸synchronized銈傚父銇康闋伀缃亜銇︺亰銇嶃仧銇勩仹銇欍伃銆

Extension銇娇銇勩仼銇撱倣

銇撱亾銇俱仹銇с倧銈娿仺绨″崢銇獷xtension銈掍娇銇堛倠銇撱仺銇屻倧銇嬨仯銇熴亱銇ㄦ濄亜銇俱仚銆

ActorSystem銇斻仺銇紤銈ゃ兂銈广偪銉炽偣銇ㄣ亜銇嗐亾銇ㄣ倐銈忋亱銈娿伨銇椼仧銆

銇с仚銇屻佸晱椤屻伅浣裤亜銇┿亾銈嶃仹銇欍傚牬鍚堛伀銈堛仯銇︺伅鍗樸伀object CountExtensionImpl銇仼銇ㄣ仐銇︺佹櫘閫氥伀銈枫兂銈般儷銉堛兂銇偑銉栥偢銈с偗銉堛倰浣溿仯銇熸柟銇岃壇銇勩亱銇ㄦ濄亜銇俱仚銆

銇┿亞銇勩亞銇ㄣ亶銇獷xtension銈掍娇銇嗐伖銇嶃仾銇亱銆丱SS銇嬨倝瀛︺倱銇с伩銇俱仚銆

okumin/akka-persistence-sql-async

https://github.com/okumin/akka-persistence-sql-async

ScalikejdbcExtensions銇ㄣ亜銇嗐倐銇倰浣溿仯銇︺亜銇俱仚銇

https://github.com/okumin/akka-persistence-sql-async/blob/964962e2e7d09b164a14e7a532b6712cc344181c/core/src/main/scala/akka/persistence/common/ScalikeJDBCExtension.scala

銇撱倢銈掑垾銇瓵ctor銇嬨倝鍒╃敤銇椼仸銇勩伨銇欍

https://github.com/okumin/akka-persistence-sql-async/blob/fda57d61bec1a23389913a14dfbd5ae288da102c/core/src/main/scala/akka/persistence/common/StoragePlugin.scala#L13-L14

Setting銇叡鏈夌洰鐨勩仹浣裤仯銇︺亜銈嬨倛銇嗐仹銇欍伃銆

ktoso/akka-raft

https://github.com/ktoso/akka-raft

DefaultLogCompaction銇ㄣ亜銇嗐倐銇倰浣溿仯銇︺亜銇俱仚銆

https://github.com/ktoso/akka-raft/blob/6fdfeaa79eb710ef167f5bf2198dea426a927079/src/main/scala/pl/project13/scala/akka/raft/compaction/DefaultLogCompactionSupport.scala

RaftActor鍐呫仹logCompaction澶夋暟銈掍綔鎴愩仐銇︺佹渶绲傜殑銇互涓嬨伄閮ㄥ垎銇т娇銇c仸銇勩倠銈堛亞銇с仚銆

https://github.com/ktoso/akka-raft/blob/6fdfeaa79eb710ef167f5bf2198dea426a927079/src/main/scala/pl/project13/scala/akka/raft/SharedBehaviors.scala#L148

銇撱倢銇儹銈搞儍銈倰鍏辨湁銉诲叡閫氬寲銇欍倠銇熴倎銇娇銇c仸銇勩倠銈堛亞銇с仚銇

actorapp/actor-platform

https://github.com/actorapp/actor-platform

https://github.com/actorapp/actor-platform/blob/10052cce5178eb624db91e203c7c88f0a3a8c227/actor-server/actor-rpc-api/src/main/scala/im/actor/server/api/rpc/RpcApiExtension.scala

銇撱倢銇孍xtension銇с仚銆

鍚勬墍銇嬨倝register銈掍娇銇c仸銇勩伨銇欍

services銇ㄣ亜銇嗙姸鎱嬨倰鎸併仭銇ゃ仱銆佽嚜鍓嶃仹浣滄垚銇椼仧actor銇儭銉冦偦銉笺偢銈掗併仯銇︺亜銇俱仚銆

鐘舵厠锛嬪嚘鐞嗗叡閫氬寲銇裤仧銇勩仾浣裤倧銈屾柟銇с仚銇

绲愬眬鏄庣⒑銇娇銇勩仼銇撱倣銇倧銇嬨倞銇俱仜銈撱仹銇椼仧銇屻佺姸鎱嬨倰鍏辨湁銇椼仧銇勩仺銇嶃倓銆併儭銈姐儍銉夈倰鍏辨湁銇椼仧銇勩仺銇嶃伀浣裤亞銈傘伄銇倛銇嗐仹銇欍傚嬩汉鐨勩仾鎰忚銇с仚銇屻丄ctor銇嫛寮点仺銇勩亞銈堛倞銇疉ctor銇嬨倝鍒╃敤鍙兘銇儴鍝併倰瀹氱京銇欍倠鎰熴仒銇嬨仾銉笺仺鎬濄亜銇俱仐銇熴

ActorExtension銇娇銇勩仼銇撱倣銇ㄣ亱銉°儶銉冦儓銇ㄣ亱瑭炽仐銇勬柟銇勩仧銈夈仠銇叉暀銇堛仸銇忋仩銇曘亜锛