1

我最近了解到,使用 JAX-RS 2.0 长时间运行的服务端点可以利用@Suspended注释并AsyncResponse为传入请求释放资源,而实际工作在后台完成。所有客户端示例——至少是我目前发现的那些——要么直接调用此类端点(普通的 http-call),要么使用 JAX-RS 客户端 API。但是我无法弄清楚如何将它与基于代理的 API 一起使用。

给定一个 REST 端点,它使用@Suspended

public interface HeavyLiftingService {
  @GET
  @Path("/heavylifting")
  public void heavyLifting(@Suspended final AsyncResponse aResponse);
}

它使用 Spring 的实现:

@Component
public class HeavyLiftingServiceImpl implements HeavyLiftingService {
  @Override
  @Async
  public void heavyLifting(@Suspended final AsyncResponse aResponse) {
    final Result result = doHeavyLifting();
    aResponse.resume(result);
  }
}

还有一个基于代理的客户端,它想要获得结果:

HeavyLiftingService proxy = JAXRSClientFactory.create("https://some-server.xyz", HeavyLiftingService.class);
proxy.heavyLifting(null); // what to put in here?
Result result = null; // how can I get the result?

显然有两个问题:

  1. 我需要向heavyLifting方法提供什么作为AsyncResponse参数的值?
  2. @Suspended由于使用的方法的返回类型必须为 void ,我怎样才能得到结果?

另一个问题是如何处理服务方法中的异常。异常会自动恢复响应并返回相应的错误状态吗?

4

0 回答 0