akka actor父子監管的實現

2021-09-24 02:32:17 字數 1645 閱讀 2326

akka中,父actor可以定義supervisorstartegy來實現對子actor的異常監管應對策略。

*   override val supervisorstrategy = oneforonestrategy(maxnrofretries = 10, withintimerange = 1 minute)
以上為乙個監管策略的具體實現,可以看到具體的異常應對策略有resume,restart,stop和escalate。

當父actor生成子actor的時候,將會通過在子actor中的parent記錄父actor來傳遞異常資訊。

下面是具體的監管實現。

在子actor處理訊息的invoke()函式中,將會捕獲在receive()函式中丟擲的異常,並在handleinvokefailure()函式中進行處理。

final def invoke(messagehandle: envelope): unit = 

currentmessage = null // reset current message after successful invocation

} catch handlenonfatalorinterruptedexception finally

}

在handleinvokefailure()函式中,將會直接將錯誤資訊封裝成failed型別的系統訊息投遞到parent父actor的系統訊息佇列中。

父actor將會通過讀取系統訊息佇列的時候讀取到子actor中發生的異常。

在systeminvoke中的invokeall()函式中將會進行具體處理。

@tailrec

def invokeall(messages: earliestfirstsystemmessagelist, currentstate: int): unit =

} catch handlenonfatalorinterruptedexception

val newstate = calculatestate

// as each state accepts a strict subset of another state, it is enough to unstash if we "walk up" the state

// chain

val todo = if (newstate < currentstate) rest.reverseprepend(unstashall()) else rest

if (isterminated) sendalltodeadletters(todo)

else if (todo.nonempty) invokeall(todo, newstate)

}

在這裡,之前封裝的failed事件將會在handlefailure()函式中相應進行處理。

在handlefailure()函式中將會根據之前定義的監管策略來對此處取得的異常進行相應的處理。

final protected def handlefailure(f: failed): unit = 

}

在這裡,當配置的應對策略是escalate的時候,將會直接false,導致會直接將這個異常拋到更加高一層的actor中進行處理。

akka Actor的生命週期

通過actorsystem的actorof方法建立乙個新的actor並返回其actorref 該actor的path被確定則不會在被分配出去 該actor擁有乙個隨機的uid 該actor是乙個actor例項 在例項的過程中呼叫該actor的prestart方法。val system actorsy...

QOS的流量監管

流量監管就是對分類後的流採取某種動作,用於限制出入網路的流量速率,其原理是限制進入某一網路的某一連線的流量與突發情況。在報文滿足一定的條件時,如果某個連線的報文流量過大,流量監管就可以對該報文採取不同的處理動作,例如 丟棄報文或重新設定報文的優先順序等,通常用的的方法是使,car來限制某類報文的流量...

父子的衝突

子類可以定義父類中的同名成員 子類中的成員將隱藏父類中的同名成員 父類中的同名成員依然存在於子類中 通過作用域分辨符 訪問父類中的同名成員 child c c.mi 100 子類中的mi c.parent mi 1000 父類中的mi 父子間的衝突.cpp 此檔案包含 main 函式。程式執行將在此...