Jersey客户端异步POST请求不等待响应 [英] Jersey client async POST request to not wait for response

查看:709
本文介绍了Jersey客户端异步POST请求不等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的Jersey客户端,它能够成功地执行带有效负载的POST请求。但是现在它等待来自http端点的响应:

I have created a simple Jersey client and it is able to successfully do a POST request with a payload. But right now it waits for a response from the http endpoint:

public void callEndpoint(String endpoint, String payload) {

    try {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource webResource = client.resource(getBaseURI(endpoint));

        log.debug("Sending payload [" + payload + "] to URL - [" + getBaseURI(endpoint) + "]");

        // POST method - Is this blocking?
        // Is it possible to not wait for response here
        ClientResponse response = webResource.accept("application/json")
                                             .type("application/json")
                                             .post(ClientResponse.class, payload);
        if (response.getStatus() != 200) {
            log.error("The endpoint [" + getBaseURI(endpoint) + "] returned a non 200 status code [" + response.getStatus() + "] ");
        }

    } catch (Exception e) {
        log.error("The endpoint for " + endpoint + " - " + getBaseURI(endpoint) + " is not reachable. This is the exception - " + e);
    }

}

private URI getBaseURI(String endpoint) {
    // Get this URI from config
    String URL = "http://www.somewhere.com/v2/" + endpoint;
    return UriBuilder.fromUri(URL).build();
}

问题:代码是否可能不等待回复。

我试图阅读泽西岛客户端文档,以查找我的代码是否可以不等待响应?我看到我们只能在读取响应后关闭连接,但在我的情况下它没用。我想在将有效负载发布到端点后立即关闭连接。

I was trying to read the Jersey client docs to find if its possible for my code to not wait for the response? I saw that we can only close the connection once we read the response but its not useful in my case. I want to close the connection as soon as I post the payload to the endpoint.

我只需触发并忘记POST请求,因为我不关心响应。这是因为处理在该端点上花费了大量时间,我不希望线程等待处理。

I just need to fire and forget the POST request as I don't care about the response. This is because the processing takes a lot of time at that endpoint and I don't want the thread to wait for the processing.

还可以等待某些请求的响应但不是全部吗?我可以在客户端设置一个参数,使其等待/不等待吗?我还在阅读java文档,所以这可能是一个非常简单的设置,但我到目前为止无法找到这样问。谢谢!

Also is it possible to wait for the response for some of the requests but not for all? Is there a parameter that I can set in the client that makes it wait/not wait? I am still reading the java docs so this might be a very simple setting but I wasn't able to find till now so asking here. Thanks!

[更新]

我使用以下代码,但是我运行我的java示例代码,它打印 start&立即完成但程序会继续运行一段时间然后退出。我猜它在等待Future的响应,所以我可以让我的脚本不等待它吗?代码是:

I got it working with the following code but when I run my java example code it prints start & done immediately but the program keeps running for a while and then exits. I am guessing its waiting for the Future response so is it possible that I can make my script not wait for it? The code is:

public static void callEndpoint(String endpoint, String payload) {

    try {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        AsyncWebResource webResource = client.asyncResource(getBaseURI(endpoint));

        // POST method
        System.out.println("start");
        Future<ClientResponse> resp = webResource.accept("application/json")
                .type("application/json")
                .post(ClientResponse.class, payload);
        // This line makes the code wait for output
        //System.out.println(resp.get()); 

    } catch (Exception e) {

        System.out.println ("The endpoint for " + endpoint + " - " + getBaseURI(endpoint) + " is not reachable. This is the exception - " + e);
    }
    System.out.println("done");
}


推荐答案

我使用了TypeListener来制作它真的异步。收到响应时将调用onComplete方法。

I used a TypeListener to make it really asynchronous. The onComplete method would be called when the response is received.

webResource.post(new TypeListener<ClientResponse>(ClientResponse.class) {
             @Override
             public void onComplete(Future<ClientResponse> f) throws InterruptedException {
                 try {
                    ClientResponse response = f.get();
                    if (response == null || response.getClientResponseStatus() != Status.OK) {
                        System.err.println(" Async " + response.getClientResponseStatus()+" "+response.getStatus());
                    } else{
                        System.out.println(" Async " + response.getClientResponseStatus()+" "+response.getStatus()+" "+response.getEntity(String.class));
                    }
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
         });

这篇关于Jersey客户端异步POST请求不等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆