2015-06-26 56 views
1

我有一个程序应该能够实现非常快速的http请求。请求应该是异步的,以便它不会阻塞主线程。在JAVA中快速和异步地制作多个http请求

所以,我创建了一个队列,由10个独立的线程发出http请求。如果在队列中插入某些内容,则获取数据的第一个线程将发出请求并处理结果。

队列中充满了数千个项目,所以多线程真的需要尽可能快地获得响应。

因为我有很多的代码,我会举一个简短的例子。

主类

package fasthttp; 

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

public class FastHTTP { 

    public static void main(String[] args) { 
     ExecutorService executor = Executors.newFixedThreadPool(10); 

     for (int i = 0; i < 10; i++) { 
      LinkedBlockingQueue queue = new LinkedBlockingQueue(); 
      queue.add("http://www.lennar.eu/ip.php");//for example 
      executor.execute(new HTTPworker(queue)); 
     } 
    } 

} 

FastHTTP类

package fasthttp; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.concurrent.LinkedBlockingQueue; 

public class HTTPworker implements Runnable { 

    private final LinkedBlockingQueue queue; 

    public HTTPworker(LinkedBlockingQueue queue) {   
     this.queue = queue; 
    } 

    private String getResponse(String url) throws IOException { 

     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

     StringBuilder response; 
     try (BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream()))) { 
      String inputLine; 
      response = new StringBuilder(); 
      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
     } 
     return response.toString(); 
    } 

    @Override 
    public void run() { 
     while (true) { 
      try { 
       String data = (String) queue.take(); 
       String response = getResponse(data); 
       //Do something with response 
       System.out.println(response); 
      } catch (InterruptedException | IOException ex) { 
       //Handle exception 
      } 
     } 
    } 
} 

是否有更好更快的方法,使成千上万的HTTP请求异步响应处理?速度和表现是我所追求的。

+0

如果你需要这个负载测试:去和使用JMeter – Marged

+0

阿帕奇,泽西和许多其他项目支持开箱即用的异步HTTP请求。例如,请参阅http://stackoverflow.com/questions/3142915/how-do-you-create-an-asynchronous-http-request-in-java。 Google为“Java异步HTTP”。吞吐量受线程数量的限制,因为每个线程都会阻塞HTTP请求。 –

+0

看看[bayou异步http客户端](http://bayou.io/release/0.9/docs/http/Http_Client.html) – ZhongYu

回答

0

回答我自己的问题。试过Apaches异步HTTP客户端,但过了一段时间,我开始使用Ning的异步客户端,我很满意。

+0

嗨Ikallas,基于项目:https://github.com/AsyncHttpClient/async-http-client有不同的方法,你使用了哪一种? – navy1978