Play Framework:处理控制器承诺超时

时间:2015-11-11 08:32:45

标签: java playframework promise akka

我所有应用程序的控制器(Play Framework 2.2.4,Java)都返回F.Promise:现在有一个控制器,我切换到另一个akka上下文,因为它的计算时间可能更长。上下文切换非常简单,因为您可以使用api:

return F.Promise.promise(...,executionContext)

并在application.conf中配置自定义executionContext。现在问题来自于计算时间超过指定的时间(让它变成20秒):我无法使Promise超时。考虑到我不想调用get (20,TimeUnit.SECONDS)对promise的方法,因为我确信play会在guts中做到这一点。我在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秒。(超时方法文档非常混乱)

我尝试的其他代码段是:

F.Promise<Result> original = F.Promise.promise(...,customExecutionService);
  return F.promise.timeout(original,10,TimeUnit.SECONDS).get(10,TimeUnit.SECONDS);

但这超出了外部Wrapper的承诺。

所以我现在得出的结论是,我必须编写自己的自定义executionService来设置超时监听器并自行抛出TimeoutException,但我确信有一些更简单的路径可以实现这一点

1 个答案:

答案 0 :(得分:0)

我通过使用以下代码包装原始响应(没有超时的响应)找到了一个解决方案:

Future<T> error = after(Duration.create(delay,unit),Akka.system().scheduler(),main,
                Futures.failed(new TimeoutException("Future timed out")));

return Promise.wrap(Futures.firstCompletedOf(
    Lists.newArrayList(original.wrapped(),error), main));

```

是主执行上下文,取自。基本上,如果原始承诺超过持续时间,则会在错误中通过抛出超时异常来启动。

我把它放在一个过滤器中,所以用该过滤器注释的每个控制器都有相同的行为。