【发布时间】:2015-11-12 17:10:40
【问题描述】:
我的应用程序的所有控制器(Play Framework 2.2.4,Java)都返回一个 F.Promise:现在有一个控制器可以切换到另一个 akka 上下文,因为它的计算时间可能更长。上下文切换非常简单,因为您可以使用 api:
return F.Promise.promise(...,executionContext)
并在 application.conf 中配置自定义 executionContext。现在问题来了,当计算时间长于指定的时间(让我们让它 20 秒):我无法让 Promise 超时。考虑到我不想调用 get(20,TimeUnit.SECONDS ) 方法,因为我确信 play 会在它的胆量中做到这一点。我在 akka executionService 中搜索了一些配置属性并找到了这个
thread-pool-executor {
# Keep alive time for threads
keep-alive-time = 60s
# Min number of threads to cap factor-based core number to
core-pool-size-min = 8
# The core pool size factor is used to determine thread pool core size
# using the following formula: ceil(available processors * factor).
# Resulting size is then bounded by the core-pool-size-min and
# core-pool-size-max values.
core-pool-size-factor = 3.0
# Max number of threads to cap factor-based number to
core-pool-size-max = 64
# Minimum number of threads to cap factor-based max number to
# (if using a bounded task queue)
max-pool-size-min = 8
# Max no of threads (if using a bounded task queue) is determined by
# calculating: ceil(available processors * factor)
max-pool-size-factor = 3.0
# Max number of threads to cap factor-based max number to
# (if using a bounded task queue)
max-pool-size-max = 64
# Specifies the bounded capacity of the task queue (< 1 == unbounded)
task-queue-size = -1
# Specifies which type of task queue will be used, can be "array" or
# "linked" (default)
task-queue-type = "linked"
# Allow core threads to time out
allow-core-timeout = on
}
但 keep-alive-time 和 allow-core-timeout 属性都不能用于超时长计算承诺。
我尝试将返回类型更改为:
F.Promise<Result> original = F.Promise.promise(...,customExecutionService);
return F.promise.timeout(original,10,TimeUnit.SECONDS);
但这只是将执行推迟了 10 秒。(超时方法文档非常混乱)
我试过的其他 sn-p 是:
F.Promise<Result> original = F.Promise.promise(...,customExecutionService);
return F.promise.timeout(original,10,TimeUnit.SECONDS).get(10,TimeUnit.SECONDS);
但这会使外部 Wrapper 承诺超时。
所以我现在得出的结论是,我必须编写自己的自定义 executionService 来设置超时侦听器并自行引发 TimeoutException,但我确信有一些更简单的方法可以实现这一目标.
【问题讨论】:
标签: java playframework promise akka