有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java嵌套超时处理程序在响应中不工作

我正在尝试对Jersey异步端点执行以下操作:

  • 为响应设置超时和超时处理程序

  • 如果超时,请设置一个新的处理程序并再次尝试生成响应

  • 最后,如果响应失败,返回503

在尝试模拟这个场景时,我注意到内部响应的超时处理程序没有被触发,这是Jersey的已知行为吗?下面是一段代码片段,解释了我试图实现的目标

 @GET
  @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  @ManagedAsync
  public void getMyResponseAsync(@Context SecurityContext securityContext, @Suspended AsyncResponse response) throws Exception {

    response.setTimeout(1, TimeUnit.MINUTES);

    response.setTimeoutHandler((timedoutResponse) -> {

      logger.info("request timed out, attempting to generate a new one");
      timedoutResponse.setTimeout(1, TimeUnit.MINUTES);
      timedoutResponse.setTimeoutHandler((finalResponse) -> {
          //this handler is never triggered even if the calculation goes beyond 1 minute
          logger.info("request timed out after a second attempt");
          finalResponse.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE).build());
        }
      );
      //takes around 6mins but the handler is not trigged 
      simulateLongCalculationToTriggerTimeout();

      String value = service.getValue(value);

        timedoutResponse.resume(value);

    });

     //this triggers the first timeout
     simulateLongCalculationToTriggerTimeout();

    String value = service.getValue(value);

    response.resume(value);
  }

共 (0) 个答案