【发布时间】:2016-07-20 09:14:14
【问题描述】:
这里是新手..
通过 Java API 使用 akka 版本:akka-actor_2.11(2.4.8)。
我正在尝试开发一个用于生成 PDF 文档的演员。这些 PDF 文档可能很大,所以很明显我想限制参与者处理请求的速率。此外,作为附带要求,我还需要一个“优先级”收件箱,通过该收件箱可以根据底层参与者的优先级处理 PDF 生成请求。
在我的应用程序启动中,我创建了一个像这样的全局道具:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
然后我为每个 pdf 请求创建演员,如下所示:
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
我的 application.conf 如下所示:
prio-dispatcher {
mailbox-type = "com.x.y.config.PriorityMailbox"
}
我的 PriorityMailbox 如下所示:
public class PriorityMailbox extends UnboundedPriorityMailbox {
// needed for reflective instantiation
public PriorityMailbox(final ActorSystem.Settings settings, final Config config) {
super(new PriorityGenerator() {
@Override
public int gen(final Object message) {
System.out.println("Here is my message to be prioritized: "+message);
if (message instanceof Prioritizable) {
Prioritizable prioritizable = (Prioritizable) message;
if (prioritizable.getReportPriorityType() == ReportPriorityType.HIGH) {
return 0;
} else if (prioritizable.getReportPriorityType() == ReportPriorityType.LOW) {
return 2;
} else if (message.equals(PoisonPill.getInstance())) {
return 3; // PoisonPill when no other left
} else {
return 1;
}
} else {
// Default priority for any other messages.
return 1;
}
}
});
}
}
这是实现我想要的正确配置吗?我不确定我是否遗漏了什么。首先,我在邮箱实现中看不到任何 System.out.prints。我想它应该来那里比较优先级。
其次,我希望 PdfGenerationActor 按顺序(一个接一个)执行,因为它本质上是整个系统中的单个实例。但我不认为会发生这种情况。我看到多个参与者同时处理请求。
我认为我在这里遗漏了一些基本的东西。
【问题讨论】:
标签: java scala akka reactive-programming actor