如果自定义线程池队列已满,如何拒绝呼叫?



使用 Play Framework 2.5,如果池和队列完全繁忙,如何使自定义线程池拒绝任务?

使用默认和自定义线程池的下一个配置。

akka {
actor {
default-dispatcher {
throughput=1
executor="thread-pool-executor"
thread-pool-executor {
fixed-pool-size=off
core-pool-size=5
max-pool-size=10
task-queue-size=5
}
}
}
}
contexts {
custom {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size=10
task-queue-size=1
}
}
}

下一个控制器:

import akka.actor.ActorSystem;
import play.Logger;
import play.mvc.Result;
import scala.concurrent.ExecutionContextExecutor;
import javax.inject.Inject;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import static play.mvc.Results.ok;
public class TestController {
private final ExecutionContextExecutor executor;
@Inject
public TestController(ActorSystem actorSystem) {
this.executor = actorSystem.dispatchers().lookup("contexts.custom");
}
public CompletionStage<Result> call() {
return CompletableFuture.supplyAsync(this::task, executor);
}
private Result task() {
Logger.info("Task started");
sleep(5000);
Logger.info("----- Task completed ----- ");
return ok("ok");
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (Exception e) {
}
}
}

我希望在 10 个请求之后,它们将开始被自定义线程池拒绝,但相反,它们会在默认池上执行。

[application-contexts.custom-14] INFO  application - Task started
[application-contexts.custom-16] INFO  application - Task started
[application-contexts.custom-15] INFO  application - Task started
[application-contexts.custom-20] INFO  application - Task started
[application-contexts.custom-19] INFO  application - Task started
[application-contexts.custom-17] INFO  application - Task started
[application-contexts.custom-21] INFO  application - Task started
[application-contexts.custom-18] INFO  application - Task started
[application-contexts.custom-22] INFO  application - Task started
[application-contexts.custom-23] INFO  application - Task started
[application-akka.actor.default-dispatcher-7] INFO  application - Task started
[application-akka.actor.default-dispatcher-10] INFO  application - Task started
[application-akka.actor.default-dispatcher-6] INFO  application - Task started
[application-akka.actor.default-dispatcher-9] INFO  application - Task started
[application-akka.actor.default-dispatcher-4] INFO  application - Task started
[application-akka.actor.default-dispatcher-12] INFO  application - Task started
[application-akka.actor.default-dispatcher-13] INFO  application - Task started
[application-akka.actor.default-dispatcher-2] INFO  application - Task started
[application-akka.actor.default-dispatcher-8] INFO  application - Task started
[application-akka.actor.default-dispatcher-5] INFO  application - Task started
[application-akka.actor.default-dispatcher-3] INFO  application - Task started
[application-akka.actor.default-dispatcher-11] INFO  application - Task started

这意味着默认池将繁忙,除非它完成所有繁重的任务,否则不会处理其他请求。 我希望我的自定义池在池已满且队列已满时拒绝任务。 这可能吗?

您可以使用所需的逻辑以编程方式创建执行上下文,例如使用ExecutionContext.fromExecutor(new ThreadPoolExecutor(...)). 您也可以实现自己的akka.dispatch.ExecutorServiceConfigurator并在配置中指定其完全限定的类名:文档在这里

my-thread-pool-dispatcher {
# Dispatcher is the name of the event-based dispatcher
type = Dispatcher
# What kind of ExecutionService to use
executor = "<Class name of the configurator>"
}

相关内容

最新更新