且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正确处理运动衫中的后台通话

更新时间:2022-11-06 08:56:35

(而不是尝试实现非阻塞" Future.get()(使用另一个线程),您可以简单地使使用 InvocationCallback ,将实例传递给 get 方法.例如

Instead of trying to implement a "non-blocking" Future.get() (with another thread), you can simply make use of the InvocationCallback, passing an instance to the get method. For example

Future<Response> future = target.request().async().get(new InvocationCallback<Response>(){
    @Override
    public void completed(Response response) {
        System.out.println(response.readEntity(String.class));
        response.close();
        client.close();
    }

    @Override
    public void failed(Throwable throwable) { /** Log exception **/ }
});

请参见异步客户端回调