Akka Notes - Introducing Actors

What are Actors?

Akka's Actors follow the Actor Model (duh!).
Treat Actors like People. People who don't talk to each other in person. They just talk through mails.
Let's expand on that a bit.

1. Messaging

Consider two persons - A wise Teacher and Student. The Student sends a mail every morning to the Teacher and the wise Teacher sends a wise quote back.
Points to note :

  • The student sends a mail. Once sent, the mail couldn't be edited. Talk about natural immutability.
  • The Teacher checks his mailbox when he wishes to do so.
  • The Teacher also sends a mail back (immutable again).
  • The student checks the mailbox at his own time.
  • The student doesn't wait for the reply. (no blocking)

That pretty much sums up the basic block of the Actor Model - passing messages.


Akka Messaging.PNG

2. Concurrency

Now, imagine there are 3 wise teachers and 3 students - every student sends notes to every other teacher. What happens then? Nothing changes actually. Everybody has their own mailbox. One subtle point to note here is this :

By default, Mails in the mailbox are read/processed in the order they arrived.

Internally, by default it is a ConcurrentLinkedQueue. And since nobody waits for the mail to be picked up, it is simply a non-blocking message. (There are a variety of built-in mailboxes including bounded and priority based. In fact, we could build one ourself too)

Akka concurrency.PNG

3. Failover

Imagine these 3 teachers are from three different departments - History, Geography and Philosophy.
History teachers replies with a note on an Event in the past, Geography teachers sends an Interesting Place and Philosophy teachers, a quote. Each student sends message to each teacher and gets responses. The student doesn't care which teacher in the department sends the reply back. What if one day, a teacher falls sick? There has to be at least one teacher handling the m
ails from the department. In this case, another teacher in the department steps up and does the job.

Akka Failover.PNG

Points to note :
There could be a pool of Actors who does different things.
An Actor could do something that causes an exception. It wouldn't be able to recover by itself. In which case a new Actor could becreated in place of the old one. Alternatively, the Actor could just ignore that one particular message and proceed with the rest of the messages. These are called Directives and we'll discuss them later.

4. Multitasking

For a twist, let's assume that each of these teachers also send the exam score through mail too, if the student asks for it. Similarly, an the Actor could handle more than one type of message comfortably.

5. Chaining

What if the student would like to get only one final consolidated trivia mail instead of three?
We could do that too with Actors too. We could chain the teachers as a hierarchy. We'll come back to that later when we talk about Supervisors and revisit the same thought when we talk about Futures.
As requested by Mohan, let's just try to map the analogy components with the the components in the Actor Model.


AkkaComponentMatching.png

Students and the Teachers becomes our Actors. The Email Inbox becomes the Mailbox
component. The request and the response can't be modified. They are immutable objects. Finally, the MessageDispatcher component in Actor manages the mailbox and routes the messages to the respective Mailbox.
Enough talk, let's cook up some code....

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坡倔,一起剝皮案震驚了整個濱河市尸闸,隨后出現(xiàn)的幾起案子嬉荆,更是在濱河造成了極大的恐慌砰盐,老刑警劉巖展蒂,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異座菠,居然都是意外死亡蠢琳,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進(jìn)店門疑苔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甫匹,“玉大人,你說我怎么就攤上這事惦费”福” “怎么了?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵薪贫,是天一觀的道長恍箭。 經(jīng)常有香客問我,道長瞧省,這世上最難降的妖魔是什么扯夭? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮鞍匾,結(jié)果婚禮上交洗,老公的妹妹穿的比我還像新娘。我一直安慰自己橡淑,他們只是感情好构拳,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般置森。 火紅的嫁衣襯著肌膚如雪斗埂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天凫海,我揣著相機(jī)與錄音呛凶,去河邊找鬼。 笑死行贪,一個胖子當(dāng)著我的面吹牛漾稀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瓮顽,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼县好,長吁一口氣:“原來是場噩夢啊……” “哼围橡!你這毒婦竟也來了暖混?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤翁授,失蹤者是張志新(化名)和其女友劉穎拣播,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體收擦,經(jīng)...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贮配,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了塞赂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片泪勒。...
    茶點(diǎn)故事閱讀 38,650評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖宴猾,靈堂內(nèi)的尸體忽然破棺而出圆存,到底是詐尸還是另有隱情,我是刑警寧澤仇哆,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布沦辙,位于F島的核電站,受9級特大地震影響讹剔,放射性物質(zhì)發(fā)生泄漏油讯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一延欠、第九天 我趴在偏房一處隱蔽的房頂上張望陌兑。 院中可真熱鬧,春花似錦由捎、人聲如沸兔综。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邻奠。三九已至笤喳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碌宴,已是汗流浹背杀狡。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贰镣,地道東北人呜象。 一個月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像碑隆,于是被迫代替她去往敵國和親恭陡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評論 2 349

推薦閱讀更多精彩內(nèi)容