在Java中点火并忘记HTTP

时间:2016-07-09 01:44:39

标签: java httprequest httpclient analytics httpresponse

我们正在实施自己的分析,因为我们已经公开了一个需要调用的Web服务,它将捕获数据库中的数据。

问题在于,由于这是分析,我们会进行大量调用(例如每个页面加载,在每个js之后调用,CSS加载等等),因此会有很多这样的调用。因此,我不希望服务器加载大量请求,以便更准确地等待响应。因为我们得到的回复对我们来说几乎没有任何用处。

那么有没有办法解雇网络服务请求并忘记我已经解雇了它?

我知道每个HTTP请求都会有响应。

因此,如果我们将请求超时设置为零秒,那么我想到的一件事是什么?但我不确定这是否是正确的做法。

请向我提供更多建议

3 个答案:

答案 0 :(得分:2)

您可能会发现以下AsyncRequestDemo.java有用:

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.http.client.fluent.Async;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.concurrent.FutureCallback;

/**
 * Following libraries have been used:
 * 
 * 1) httpcore-4.4.5.jar
 * 2) httpclient-4.5.2.jar
 * 3) commons-logging-1.2.jar
 * 4) fluent-hc-4.5.2.jar     * 
 *
 */

public class AsyncRequestDemo {
    public static void main(String[] args) throws Exception {
    URIBuilder urlBuilder = new URIBuilder()
                               .setScheme("http")
                               .setHost("stackoverflow.com")
                               .setPath("/questions/38277471/fire-and-forget-for-http-in-java");

    final int nThreads = 3; // no. of threads in the pool
    final int timeout = 0; // connection time out in milliseconds

    URI uri = null;
    try {
        uri = urlBuilder.build();
    } catch (URISyntaxException use) {
        use.printStackTrace();
    }

    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    Async async = Async.newInstance().use(executorService);
    final Request request = Request.Get(uri).connectTimeout(timeout);

        Future<Content> future = async.execute(request, new FutureCallback<Content>() {
            public void failed(final Exception e) {
                System.out.println("Request failed: " + request);
                System.exit(1);
            }

            public void completed(final Content content) {
                System.out.println("Request completed: " + request);
                System.out.println(content.asString());
                System.exit(0);
            }

            public void cancelled() {
            }
        });

        System.out.println("Request submitted");

    }

}

答案 1 :(得分:1)

我用过这个:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

  URL url = new URL(YOUR_URL_PATH, "UTF-8")); 
               ExecutorService executor = Executors.newFixedThreadPool(1); 
               Future<HttpResponse> response = executor.submit(new HttpRequest(url));
               executor.shutdown();

用于HttpRequest,HttpResponse

public class HttpRequest implements Callable<HttpResponse> {
        private URL url;

        public HttpRequest(URL url) {
            this.url = url;
        }

        @Override
        public HttpResponse call() throws Exception {
            return new HttpResponse(url.openStream());
        }
}

public class HttpResponse {
     private InputStream body;

        public HttpResponse(InputStream body) {
            this.body = body;
        }

        public InputStream getBody() {
            return body;
        }
}

就是。

答案 2 :(得分:0)

是的,您可以发起请求并断开连接而无需等待回复... 您可能不想这样做。服务器端不得不处理不合理断开的连接的开销远远超过让它继续返回响应。

在Java servlet中解决此类性能问题的更好方法是将所有数据从请求推送到队列中,立即响应,并让一个或多个工作线程从队列中拾取项目以进行处理(例如将其写入数据库)。