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

标签 java post jersey jersey-client google-http-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();
}

问题:代码是否可以不等待响应

我正在尝试阅读 Jersey client docs查找我的代码是否有可能不等待响应?我看到我们只能在阅读响应后关闭连接,但它对我来说没有用。我想在将有效负载发布到端点后立即关闭连接。

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

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

[更新]

我使用以下代码让它工作,但是当我运行我的 java 示例代码时,它会立即打印 start & done 但程序会继续运行一段时间然后退出。我猜它在等待 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");
}

最佳答案

我使用 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();
                }
             }
         });

关于java - Jersey 客户端异步 POST 请求不等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29906956/

相关文章:

java - 如何以编程方式将文件上传到 Windows Server 2003 远程桌面

java - MouseListener 什么都不做

java - 如何从多线程类中正确获取静态变量?

java - Jersey 1.9 RESTful服务资源库重新定义

java - java中从多个表检索数据时的最佳设计原则

python - 如何使用 Curl 发布文件列表

javascript - 没有将值发送到邮件

python - 使用具有多个 header 的 urllib 发出 POST 请求会出现 400 Bad Request 错误

Java Restful Service eclipse tomcat HTTP错误404

java - 找不到媒体类型 Jersey MessageBodyWriter = text/plain