【发布时间】:2015-04-21 13:47:12
【问题描述】:
我是 Akka 的新手(Java lib,v2.3.9)。我正在尝试关注supervisor hierarchy best practices,但由于这是我的第一个 Akka 应用程序,我在某处遇到了心理障碍。
在我的第一个 Akka 应用程序(实际上是一个旨在跨多个应用程序重用的库)中,来自外部世界的输入表现为传递给参与者的 Process 消息。使用我的应用程序的开发人员将提供一个基于文本的配置文件,最终配置哪些参与者会被发送Process 实例,哪些不会。换句话说,假设这些是我的演员类:
// Groovy pseudo-code
class Process {
private final Input input
Process(Input input) {
super()
this.input = deepClone(input)
}
Input getInput() {
deepClone(this.input)
}
}
class StormTrooper extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof Process) {
// Process the message like a Storm Trooper would.
}
}
}
class DarthVader extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof Process) {
// Process the message like Darth Vader would.
}
}
}
class Emperor extends UntypedActor {
@Override
void onReceive(Object message) {
if(message instanceof Process) {
// Process the message like the Emperor would.
}
}
}
// myapp-config.json -> where the actors are configured, along with other
// app-specific configs
{
"fizzbuzz": "true",
"isYosemite": "false",
"borderColor": "red",
"processors": [
"StormTrooper",
"Emperor"
]
}
正如您在配置文件中看到的,只有StormTrooper 和Emperor 被选中接收Process 消息。这最终导致创建零 (0) DarthVader 演员。我还打算让Set<ActorRef> 可用于填充有StormTrooper 和Emperor 的应用程序,如下所示:
class SomeApp {
SomeAppConfig config
static void main(String[] args) {
String configFileUrl = args[0] // Nevermind this horrible code
// Pretend here that configFileUrl is a valid path to
// myapp-config.json.
SomeApp app = new SomeApp(configFileUrl)
app.run()
}
SomeApp(String url) {
super()
config = new SomeAppConfig(url)
}
void run() {
// Since the config file only specifies StormTrooper and
// Emperor as viable processors, the set only contains instances of
// these ActorRef types.
Set<ActorRef> processors = config.loadProcessors()
ActorSystem actorSystem = config.getActorSystem()
while(true) {
Input input = scanForInput()
Process process = new Process(input)
// Notify each config-driven processor about the
// new input we've received that they need to process.
processors.each {
it.tell(process, Props.self()) // This isn't correct btw
}
}
}
}
所以,正如您(希望)所见,我们拥有所有这些参与者(实际上,有几十个 UntypedActor impls)来处理 Process 消息(反过来,从某些来源捕获 Input) .至于哪些 Actor 还活着/在线来处理这些Process 消息完全是配置驱动的。最后,应用程序每次收到Input 时,都会将其注入到Process 消息中,并将Process 消息发送给所有已配置/活动的actor。
有了这个作为给定的背景故事/设置,我无法确定“演员/主管层次结构”需要是什么。在我的用例中,似乎所有参与者都是真正平等的,他们之间没有监督结构。 StormTrooper 只是接收Process 消息,如果该类型的参与者被配置为存在。其他actor子类也一样。
我在这里完全错过了什么吗?如果所有参与者都是平等的并且层次结构本质上是“扁平”/水平的,我该如何定义监督层次结构(出于容错目的)?
【问题讨论】:
-
借用 Viktor Klang 的一个思考过程,想想如果你只有普通人而不是电脑你会怎么做,然后把每个人想象成一个演员。哪些人监督哪些人?
-
谢谢@Ryan (+1) - 如果我有真正的人作为演员,而不是电脑,我会有一个带有
StormTrooper、DarthVader和里面的Emperor的牢房.当有人想向这个牢房的居民发送信息时,他们必须在一张纸上写下相同的信息,每人一张纸。如果他们希望同时向StormTrooper和Emperor发送消息,他们会在两张纸上写下完全相同的消息。然后他们会将所有/任何文件交给我,我会将信息传递给适用的各方。 -
换句话说,这里仍然没有主管层次结构。 :-)
标签: java akka actor fault-tolerance akka-supervision