目前,我有收到一些消息,並保存該消息的內容可變類級別的變量的演員:應該避免Akka Actor中的可變狀態嗎?
class MyActor extends Actor {
val items1: Seq[Item] = _
val items2: Seq[Item] = _
def receive = {
case Start(items1, items2) =>
items1= items1
items2 = items2
case Result(...) => ...
}
private def method1(args...) = {
// do something with items1 and items2
}
private def method2(args...) = {
// do something with items1 and items2
}
}
和我讀到它真的好創建多個receive
功能和使用context.become
/unbecome
處理不同的州。 所以在我的情況下,我可以用receive
功能如下:
def running(items1: Seq[Item], items2: Seq[Item]): Receive = {
case Result(...) => ...
}
val waiting: Receive = {
case Start(items1, items2) =>
context.become(running(items1, items2))
start()
}
這種做法在我看來很清晰,美觀大方,但問題是,我有其他的方法(method1
和method2
),它需要這個items1
和items2
變量。我認爲唯一可能的解決方法是將items1
和items2
作爲函數傳遞給method1
和method2
,但此方法已經有2-3個參數,並且向它們添加更多參數會使它們難以閱讀(實際上我有2個以上的參數方法)。
那麼,在這種情況下Akka是否有任何模式?或者,在課堂級別var
(就像我的第一個例子)那樣,這很「合法」?
調用'method1'和'method2'的方法或時間? –