首先使用nutz并发http,代码如下,1000个
@Test
public void t12() throws ExecutionException, InterruptedException {
int taskSize = 1000;
Sender.setup(null);
List<Future> list = new ArrayList<>();
for (int i = 0; i < taskSize; i++) {
Future<Response> response = Sender.create("http://rc.uptechind.cn/ne").send(new Callback<Response>() {
Stopwatch sw = Stopwatch.begin();
@Override
public void invoke(Response res) {
sw.stop();
System.out.println(res.getStatus());
System.out.println(String.format("线程[%s] %s", Thread.currentThread().getName(), sw.toString()));
}
});
list.add(response);
}
int i = 0;
for (Future f : list) {
Response res = (Response) f.get();
System.out.println(++i);
}
Sender.shutdown();
}
执行一会后,没有执行完毕,就报错
java.util.concurrent.ExecutionException: org.nutz.http.HttpException: url=http://rc.uptechind.cn/ne
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at test.sunyu.tools.http.TestNutzHttp.t12(TestNutzHttp.java:254)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
Caused by: org.nutz.http.HttpException: url=http://rc.uptechind.cn/ne
at org.nutz.http.sender.GetSender.send(GetSender.java:22)
at org.nutz.http.Sender.call(Sender.java:236)
at org.nutz.http.Sender.call(Sender.java:38)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1169)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:933)
at sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2662)
at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2584)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1770)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.nutz.http.Sender.getResponseHeader(Sender.java:137)
at org.nutz.http.sender.GetSender.send(GetSender.java:19)
... 6 more
再来看看HttpClient的,使用连接池。一万个
@Test
public void t6() throws ExecutionException, InterruptedException {
Stopwatch sw = Stopwatch.begin();
int taskSize = 10000;
ExecutorService executorService = Executors.newFixedThreadPool(taskSize);
List<Future> list = new ArrayList<Future>();
for (int i = 0; i < taskSize; i++) {
Future f = executorService.submit(new MyHttpCallable2(i + ""));
list.add(f);
}
for (Future f : list) {
System.out.println(f.get());
}
sw.stop();
System.out.println(String.format("总用时:" + sw.toString()));
executorService.shutdown();
}
private class MyHttpCallable2 implements Callable<String> {
String threadName;
public MyHttpCallable2(String threadName) {
this.threadName = threadName;
}
@Override
public String call() throws Exception {
Stopwatch sw = Stopwatch.begin();
httpTools.get("http://rc.uptechind.cn/ne");
sw.stop();
return String.format("线程[%s] %s", threadName, sw.toString());
}
}
由于后台打印太多,所以直接打印后几个。看出来,10000个链接,全都执行完毕,没有错误。
线程[9981] Total: 99679ms : [2016-08-29 16:48:20.514]=>[2016-08-29 16:50:00.193]
线程[9982] Total: 106159ms : [2016-08-29 16:48:20.514]=>[2016-08-29 16:50:06.673]
线程[9983] Total: 106174ms : [2016-08-29 16:48:20.516]=>[2016-08-29 16:50:06.69]
线程[9984] Total: 99676ms : [2016-08-29 16:48:20.52]=>[2016-08-29 16:50:00.196]
线程[9985] Total: 83510ms : [2016-08-29 16:48:20.516]=>[2016-08-29 16:49:44.026]
线程[9986] Total: 106156ms : [2016-08-29 16:48:20.516]=>[2016-08-29 16:50:06.672]
线程[9987] Total: 57585ms : [2016-08-29 16:48:20.517]=>[2016-08-29 16:49:18.102]
线程[9988] Total: 99653ms : [2016-08-29 16:48:20.517]=>[2016-08-29 16:50:00.17]
线程[9989] Total: 99680ms : [2016-08-29 16:48:20.52]=>[2016-08-29 16:50:00.2]
线程[9990] Total: 25301ms : [2016-08-29 16:48:20.517]=>[2016-08-29 16:48:45.818]
线程[9991] Total: 99651ms : [2016-08-29 16:48:20.518]=>[2016-08-29 16:50:00.169]
线程[9992] Total: 99677ms : [2016-08-29 16:48:20.517]=>[2016-08-29 16:50:00.194]
线程[9993] Total: 106175ms : [2016-08-29 16:48:20.518]=>[2016-08-29 16:50:06.693]
线程[9994] Total: 83539ms : [2016-08-29 16:48:20.519]=>[2016-08-29 16:49:44.058]
线程[9995] Total: 106178ms : [2016-08-29 16:48:20.518]=>[2016-08-29 16:50:06.696]
线程[9996] Total: 83526ms : [2016-08-29 16:48:20.518]=>[2016-08-29 16:49:44.044]
线程[9997] Total: 83530ms : [2016-08-29 16:48:20.519]=>[2016-08-29 16:49:44.049]
线程[9998] Total: 83530ms : [2016-08-29 16:48:20.519]=>[2016-08-29 16:49:44.049]
线程[9999] Total: 57613ms : [2016-08-29 16:48:20.519]=>[2016-08-29 16:49:18.132]
总用时:Total: 113926ms : [2016-08-29 16:48:16.743]=>[2016-08-29 16:50:10.669]