小编oos*_*sie的帖子

如何使用 Jersey Client 2.2.x 取消挂起的异步请求并取消注册调用回调?

我有一个带有 sleep 方法的简单 REST 服务,该方法除了在指定的毫秒时间内睡眠之外不做任何事情,然后返回无内容响应。我的 RESTTest 类尝试首先调用http://localhost:8080/myapp/rest/sleep/7500(休眠 7.5 秒),但只等待 5 秒。5 秒后,它取消接收到的 Future(尝试取消挂起的请求)并调用http://localhost:8080/myapp/rest/sleep/5000(休眠 5 秒)并等待 5 秒。

public class RESTTest {
    private final Client client = ClientBuilder.newClient();
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition responseReceived = lock.newCondition();

    public static void main(final String... arguments) {
        new RESTTest().listen(10000);
    }

    public void listen(final long time) {
        System.out.println("Listen for " + time + " ms.");
        Future<Response> _response =
            client.
                target("http://localhost:8080/myapp/rest/sleep/" + time)).
                request().
                async().
                    get(
                        new InvocationCallback<Response>() …
Run Code Online (Sandbox Code Playgroud)

java rest asynchronous jersey jersey-client

5
推荐指数
1
解决办法
1495
查看次数

标签 统计

asynchronous ×1

java ×1

jersey ×1

jersey-client ×1

rest ×1