繁体   English   中英

Java Akka Actors - 消息限制和优先级

[英]Java Akka Actors - Message throttling and priority

新手在这里..

通过Java API使用akka版本:akka-actor_2.11(2.4.8)。

我正在尝试开发一个用于生成PDF文档的actor。 这些PDF文档可能很大,所以显然我想要限制actor处理请求的速率。 另外,作为一项要求,我还需要一个“可按优先级排序”的收件箱,通过该收件箱可以根据底层参与者的优先级处理PDF生成请求。

在我的应用程序启动中,我创建了这样的全局道具:

Props.create(PdfGeneratorActor.class).withDispatcher("prio-dispatcher").withRouter(new RoundRobinPool(1))

然后我按照这样的pdf请求创建actor:

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顺序执行(逐个),因为它实际上是整个系统中的单个实例。 但我没有看到这种情况发生。 我看到多个actor同时处理请求。

我想我在这里缺少一些基本的东西。

我认为在你的情况下发生的事情是你创建的每个actor都有它自己的路由器,但是否则它们是独立的 - 所以它们并行执行。

如果您希望按顺序执行请求,那么想法是让一个路由器具有一个“worker”/ routee,逐个执行每个请求。 (当然,您可以配置要并行执行的请求数)

所以你会有这样的事情:

在conf中:

  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");
}

另请查看邮箱路由器的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM