Jersey 客户端异步 POST 请求不等待响应

Sum*_*itk 5 java post jersey google-http-client jersey-client

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

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();
}
Run Code Online (Sandbox Code Playgroud)

问题:代码是否有可能不等待响应。

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

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

是否可以等待某些请求的响应而不是所有请求的响应?是否有可以在客户端中设置的参数使其等待/不等待?我仍在阅读 java 文档,所以这可能是一个非常简单的设置,但直到现在我才找到,所以在这里问。谢谢!

[更新]

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

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");
}
Run Code Online (Sandbox Code Playgroud)

小智 2

我使用 TypeListener 使其真正异步。当收到响应时,将调用 onComplete 方法。

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();
                }
             }
         });
Run Code Online (Sandbox Code Playgroud)