Sphereとともに

scalaとかplayframeworkとか。技術ブログにしたいなと。環境は Windows7x64, mac です。たまに声優さん情報が混ざります。最近ちょっとClojure触りました。

Scala Akka 適当訳

http://doc.akka.io/api/akka/2.0/akka/actor/Actor.html

Actor base trait that should be extended by or mixed to create an Actor with the semantics of the 'Actor Model': http://en.wikipedia.org/wiki/Actor_model

ActorのベースtraitはActor Modelの意味論としてActorを作るためにextendかmixされるべきである。

An actor has a well-defined (non-cyclic) life-cycle.

actorは十分に定義された(循環しない)ライフサイクルを持っています。

RUNNING (created and started actor) - can receive messages
SHUTDOWN (when 'stop' or 'exit' is invoked) - can't do anything

The Actor's own ActorRef is available as self, the current message’s sender as sender and the ActorContext as context.
The only abstract method is receive which shall return the initial behavior of the actor as a partial function (behavior can be changed using context.become and context.unbecome).

ActorのActorRefは事故として現在のメッセージの送信者として送信者、そして、コンテキストとしてのActorContextとして利用可能です。
唯一の抽象メソッドはreceiveメソッドでそれは、部分関数としてactorの初期振る舞いを返すべきです。
(振る舞いはcontext.becomeとcontext.unbecomeを使用して変更可能です。)

class ExampleActor extends Actor {

  override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
    case _: ArithmeticException      ⇒ Resume
    case _: NullPointerException     ⇒ Restart
    case _: IllegalArgumentException ⇒ Stop
    case _: Exception                ⇒ Escalate
  }

  def receive = {
                                     // directly calculated reply
    case Request(r)               => sender ! calculate(r)

                                     // just to demonstrate how to stop yourself
    case Shutdown                 => context.stop(self)

                                     // error kernel with child replying directly to “customer”
    case Dangerous(r)             => context.actorOf(Props[ReplyToOriginWorker]).tell(PerformWork(r), sender)

                                     // error kernel with reply going through us
    case OtherJob(r)              => context.actorOf(Props[ReplyToMeWorker]) ! JobRequest(r, sender)
    case JobReply(result, orig_s) => orig_s ! result
  }
}

The last line demonstrates the essence of the error kernel design: spawn one-off actors which terminate after doing their job, pass on sender to allow direct reply if that is what makes sense, or round-trip the sender as shown with the fictitious JobRequest/JobReply message pair.

最終行はエラー核設計の本質を実証しています。
それらの仕事を終了した後に終了する一回限りのactorを生成し、
それが意味を成す場合において、送信者に直接的な返事を許可するために送信者に渡します。
もしくは、架空の JobRequest/JobReply メッセージペアで示された送信者を返します。

If you don’t like writing context you can always import context._ to get direct access to actorOf, stop etc. This is not default in order to keep the name-space clean.

もしcontextを書くことを好まないならば、import context._ と記述すれば、actorOfやstopなどを直接的に使用することが出来ます。
これはネームスペースを綺麗に保つために、デフォルトではありません。

体系的に学ぶ 安全なWebアプリケーションの作り方 脆弱性が生まれる原理と対策の実践

体系的に学ぶ 安全なWebアプリケーションの作り方 脆弱性が生まれる原理と対策の実践