我有一個程序應該能夠實現非常快速的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請求異步響應處理?速度和表現是我所追求的。
如果你需要這個負載測試:去和使用JMeter – Marged
阿帕奇,澤西和許多其他項目支持開箱即用的異步HTTP請求。例如,請參閱http://stackoverflow.com/questions/3142915/how-do-you-create-an-asynchronous-http-request-in-java。 Google爲「Java異步HTTP」。吞吐量受線程數量的限制,因爲每個線程都會阻塞HTTP請求。 –
看看[bayou異步http客戶端](http://bayou.io/release/0.9/docs/http/Http_Client.html) – ZhongYu