用于阻止请求的java Akka参与者池
我正在尝试使用线程池来发出阻塞请求。 问题是,每个请求都会阻塞整个池,并且项目会按顺序进行处理。 不确定这是否可能。谁来帮忙
city-dispatcher {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 16
}
throughput = 100
}
还有爪哇
Props props = Props.create(CityDataProcessorActor.class, psRespHolder).withDispatcher("akka.actor.city-dispatcher");
SmallestMailboxPool pool = new SmallestMailboxPool(10);
ActorRef cityRequestActorPool = actorSystem.actorOf(pool.props(props), "city-request-route");
for (String city : citiesArray) {
Future<Object> future = Patterns.ask(cityRequestActorPool, new CityCommand(city.trim()), timeout);
Object results = Await.result(future, duration);
log.info(results.toString());
}
# 1 楼答案
蒙卡拉马里的评论完全正确。这是一个实现。它会在你创建未来时创建一个列表。然后,它按顺序阻塞收集到的期货,以记录每个期货。随着迭代的进行,等待应该变得微不足道,前提是以后的未来在类似的时间内完成
# 2 楼答案
正如@Mon Calamari提到的
Object results = Await.result(future, duration);
是一个阻塞调用。你可以用callback试试futurefuture onComplete{ case Success()=> println(result) case Failure()=> println("some error") }