我正在尝试针对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);
}