【问题标题】:Java Akka Actors - Message throttling and priorityJava Akka Actors - 消息限制和优先级
【发布时间】: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


    【解决方案1】:

    我认为在您的情况下发生的情况是您创建的每个参与者都有自己的路由器,但除此之外它们是独立的 - 所以它们并行执行。

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

    所以你会有这样的东西:

    在会议中:

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

    还要查看 mailboxesrouters 的文档

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2012-05-31
      • 2012-09-04
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 2013-02-22
      • 2021-06-19
      相关资源
      最近更新 更多