【问题标题】:Play Framework: handling timeout over controller promisePlay Framework:处理控制器承诺超时
【发布时间】: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-timeallow-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


    【解决方案1】:

    我找到了一种解决方案,方法是使用以下代码包装原始响应(不会超时的响应):

    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));
    

    ```

    其中ma​​in是主要的执行上下文,after取自akka Patterns。基本上,如果原始承诺花费的时间超过在错误中传递的 duration 时间,则会通过抛出超时异常来启动。

    我将它放在一个过滤器中,因此每个使用该过滤器注释的控制器都具有相同的行为。

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2017-09-03
      • 2021-06-21
      • 1970-01-01
      • 2014-09-21
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多