注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Httpclient mutilthread  

2015-03-14 10:05:31|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

/**
*
*/
package org.example.httpclient.thread;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
* @author Daniel
*
*/
public class Test {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(400);

CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(cm).build();
try {
String https = "https://serverdomain/test/protocol";
String http = "http://serverdomain/test/protocol";
// create a thread for each URI
GetThread[] threads = new GetThread[1000];
for (int i = 1; i <= threads.length; i++) {
HttpGet httpget = new HttpGet(((i & 1) == 0) ? https : http);
threads[i - 1] = new GetThread(httpclient, httpget, i);
}

// start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
}

// join the threads
for (int j = 0; j < threads.length; j++) {
threads[j].join();
}

} finally {
httpclient.close();
}
}

/**
* A thread that performs a GET.
*/
static class GetThread extends Thread {

private final CloseableHttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
private final int id;

public GetThread(CloseableHttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
this.id = id;
}

/**
* Executes the GetMethod and prints some status information.
*/
@Override
public void run() {
try {
CloseableHttpResponse response = httpClient.execute(httpget,
context);
try {
// get the response body as an array of bytes
HttpEntity entity = response.getEntity();
if (entity != null) {
String content = EntityUtils.toString(entity);
System.out.println(id + " get result: " + content);
if (((id & 1) == 0) ? "http".equals(content) : "https".equals(content)) {
System.out
.println("***********Get Error result************* " + id + " -- " + content);
}
} else {
System.err.println("request error.");
}
} finally {
response.close();
}
} catch (Exception e) {
System.out.println(id + " - error: " + e);
}
}

}
}


  评论这张
 
阅读(349)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017