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

时间:2018-08-29 10:24:17

标签: java multithreading playframework akka threadpool

使用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

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

1 个答案:

答案 0 :(得分:0)

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

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>"

}