【发布时间】:2016-10-04 13:54:20
【问题描述】:
代码是:
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");
【问题讨论】:
标签: java asynchronous concurrency akka akka-cluster