Java Akka参与者-消息限制和优先级
我是新手 通过JavaAPI使用akka版本:akka-actor_2.11(2.4.8) 我正在尝试开发一个用于生成PDF文档的actor。这些PDF文档可能很大,因此我显然想限制参与者处理请求的速度。另外,作为一项附带要求,我还需要一个“可优先排序”的收件箱,通过它,底层参与者可以根据优先级处理PDF生成请求 在我的应用程序启动中,我创建了如下全局道具:Java Akka参与者-消息限制和优先级,java,scala,akka,reactive-programming,actor,Java,Scala,Akka,Reactive Programming,Actor,我是新手 通过JavaAPI使用akka版本:akka-actor_2.11(2.4.8) 我正在尝试开发一个用于生成PDF文档的actor。这些PDF文档可能很大,因此我显然想限制参与者处理请求的速度。另外,作为一项附带要求,我还需要一个“可优先排序”的收件箱,通过它,底层参与者可以根据优先级处理PDF生成请求 在我的应用程序启动中,我创建了如下全局道具: Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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;
}
}
});
}
}
然后根据pdf请求创建actor,如下所示:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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;
}
}
});
}
}
My application.conf如下所示:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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;
}
}
});
}
}
我的PriorityMailbox如下所示:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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按顺序(一个接一个)执行,因为它本质上是整个系统中的一个实例。但我不认为这会发生。我看到多个参与者同时处理请求
我认为我在这里遗漏了一些基本的东西。我认为在你的例子中,你创建的每个参与者都有自己的路由器,但在其他方面它们是独立的,所以它们并行执行 如果您希望您的请求按顺序执行,那么您的想法是让一个路由器和一个“工作者”/路由器逐一执行每个请求。(当然,您可以配置要并行执行的请求数) 所以你会有这样的想法:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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;
}
}
});
}
}
在配置文件中:
mypriority-mailbox {
mailbox-type = "com.x.y.config.PriorityMailbox"
mailbox-capacity = 500 #some stuff - you may want to check what you want here - if you want something
mailbox-push-timeout-time = 100s #some other stuff - check if it makes sense for you
}
actor {
/pdfRouter{
router = round-robin-pool
nr-of-instances = 1
mailbox = mypriority-mailbox
}
}
在守则中:
system.actorOf(
FromConfig.getInstance().props(PdfGeneratorActor.class),
"pdfRouter");
}
同时查看文档,我认为在您的情况下,您创建的每个参与者都有自己的路由器,但在其他方面它们是独立的,因此它们并行执行 如果您希望您的请求按顺序执行,那么您的想法是让一个路由器和一个“工作者”/路由器逐一执行每个请求。(当然,您可以配置要并行执行的请求数) 所以你会有这样的想法:
Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))
actorSystem.actorOf(propsObjShownAbove, actorType.getCanonicalName() + "_" + UUID.randomUUID());
prio-dispatcher {
mailbox-type = "com.x.y.config.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;
}
}
});
}
}
在配置文件中:
mypriority-mailbox {
mailbox-type = "com.x.y.config.PriorityMailbox"
mailbox-capacity = 500 #some stuff - you may want to check what you want here - if you want something
mailbox-push-timeout-time = 100s #some other stuff - check if it makes sense for you
}
actor {
/pdfRouter{
router = round-robin-pool
nr-of-instances = 1
mailbox = mypriority-mailbox
}
}
在守则中:
system.actorOf(
FromConfig.getInstance().props(PdfGeneratorActor.class),
"pdfRouter");
}
还要检查文档中的和