我是 Akka 的新手(Java lib,v2.3.9)。我正在尝试遵循主管层次结构的最佳实践,但由于这是我的第一个 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
impl)来处理Process
消息(这些消息Input
又从某个来源捕获)。至于哪些 Actor 还活着/在线来处理这些Process
消息完全是配置驱动的。最后,每次应用程序收到一个Input
,它都会被注入到一条Process
消息中,并且该Process
消息会发送给所有已配置/正在运行的参与者。
有了这个作为给定的背景故事/设置,我无法确定“演员/主管层次结构”需要是什么。在我的用例中,似乎所有参与者都是真正平等的,他们之间没有监督结构。如果该类型的参与者被配置为存在,则StormTrooper
只会收到一条消息。Process
其他演员子类也是如此。
我在这里完全错过了什么吗?如果所有参与者都是平等的并且层次结构本质上是“扁平”/水平的,我如何定义监督层次结构(出于容错目的)?