使用路由器的Java Akka并发限制?

时间:2016-06-04 16:04:40

标签: java asynchronous concurrency akka akka-cluster

代码是:

public class TestAkka {

    public static void main(String[] args) throws InterruptedException {

        ActorSystem system = ActorSystem.create("ExampleRouter", ConfigFactory.load().getConfig("MyRouter"));
        ActorRef router = system.actorOf(Props.create(Hello.class).withRouter(new FromConfig()), "exampleRouter");

        for (int i = 0; i < 100; i++) {
            router.tell(new Website().getNameByIndex(i), router);
        }
    }

    public static class Hello extends UntypedActor {

        @Override
        public void onReceive(Object message) throws Exception {
            if (message instanceof String) {
                System.out.println("Hello " + message);
                URL url = new URL("http://" + message + ":80");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                System.out.println(conn.getResponseCode());
                Thread.sleep(10000);  // <-- Sim the job take a short time
            } else {
                unhandled(message);
            }
        }
    }
}

application.conf是:

MyRouter{
    akka {
        actor {
            deployment {
                /exampleRouter {
                    router = round-robin-pool
                    nr-of-instances = 100
                }
            }
        }
    }
}

结果是我每次都能看到8个并发作业,但我的期望是100个并发作业应该同时运行!它仍然需要任何设置?

2016/06/06更新: 我修改了我的代码,结果是我的期望,覆盖了application.conf,它现在可以同时运行100个并发作业。实际上,如何针对高并发应用优化default-dispatcher?

String s = ""
        + "akka {\n"
        + "    actor {\n"
        + "        deployment {\n"
        + "            /router {\n"
        + "                router = round-robin-pool\n"
        + "                nr-of-instances = 10000\n"
        + "            }\n"
        + "        }\n"
        + "        default-dispatcher {\n"
        + "            fork-join-executor {\n"
        + "                parallelism-min = 200\n"
        + "                parallelism-max = 5000\n"
        + "            }\n"
        + "        }\n"
        + "    }\n"
        + "}\n";
ActorSystem as = ActorSystem.create("as", ConfigFactory.parseString(s));
ActorRef ar = as.actorOf(Props.create(Hello.class).withRouter(new FromConfig()), "router");

2 个答案:

答案 0 :(得分:0)

您对Thread.sleep的调用阻止该线程,因此您的线程已用完。如果你想看到所有100个,你应该在他们自己的专用线程上运行阻塞操作。

答案 1 :(得分:0)

您在演员数量和调度员线程之间感到困惑:

  • 路由器中的参与者数量:它是在内存中创建的实例数量,用于处理按照所选逻辑进入路由器的消息。

  • 调度程序线程:调度程序是一个线程池(或执行程序服务),负责管理线程以从代理邮箱获取消息并执行receive方法。

系统中发生的最大并发任务数将受调度程序配置的限制。让路由器(以及更多的actor来处理消息)将让这些消息由调度程序中的线程同时处理。

我建议您阅读有关akka调度员的更多信息,特别是有关默认调度员的信息:http://doc.akka.io/docs/akka/current/scala/dispatchers.html