Last active
February 2, 2018 03:39
-
-
Save elvisNg/e729d3e0a4e30972a169451e3d0ef268 to your computer and use it in GitHub Desktop.
HttpClient解决并发不足导致交易掉单问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
主要解决金融上游并发不足的问题: | |
此处分为公有和私有连接池: | |
私有连接池: | |
注:首先要在static静态池中放一个存储http连接池的Map | |
private final static Map<String,PoolingHttpClientConnectionManager> ClientPoolMap = new HashMap<String,PoolingHttpClientConnectionManager>(); | |
/** | |
* @author: WJJ | |
* @date: 2017年11月24日 上午9:33:10 | |
* @Description: TODO(调用渠道专属连接池,返回一个连接池) | |
* @param chcId 渠道专属ClientID | |
* @param soTimeout | |
* @param reqTimeout | |
* @param connectionTimeout | |
* @param maxClient | |
* @return | |
*/ | |
public static PoolingHttpClientConnectionManager getPriHttpClient(String chcId,int soTimeout,int reqTimeout,int connectionTimeout,int maxClient){ | |
PoolingHttpClientConnectionManager priClientPool = ClientPoolMap.get(chcId);//ClientPoolMap(key为chcId,value为对应的私有连接池) | |
synchronized (chcId){ | |
if(priClientPool==null){ | |
priClientPool = getNewClientPool(maxClient); //获取一个新的连接池 | |
ClientPoolMap.put(chcId, priClientPool); | |
} | |
} | |
return priClientPool; | |
} | |
/** | |
* @author: WJJ | |
* @date: 2017年11月24日 上午10:03:03 | |
* @Description: TODO(创建新的连接池) | |
* @param maxClient | |
* @return | |
*/ | |
private static PoolingHttpClientConnectionManager getNewClientPool(int maxClient){ | |
PoolingHttpClientConnectionManager priClientPool = new PoolingHttpClientConnectionManager(); | |
priClientPool.setMaxTotal(maxClient); | |
pubClientPool.setDefaultMaxPerRoute(maxClient); | |
return priClientPool; | |
} | |
/** | |
* @author: WJJ | |
* @date: 2017年11月24日 上午10:03:23 | |
* @Description: TODO(根据渠道的连接池返回一个连接) | |
* @param soTimeout | |
* @param reqTimeout | |
* @param connectionTimeout | |
* @param priClientPool | |
* @return | |
*/ | |
public static CloseableHttpClient getNewHttpClient(int soTimeout,int reqTimeout,int connectionTimeout,PoolingHttpClientConnectionManager priClientPool){ | |
CloseableHttpClient httpClient =null; | |
RequestConfig requestConfig =RequestConfig.custom() | |
.setSocketTimeout(soTimeout) | |
.setConnectTimeout(connectionTimeout) | |
.setConnectionRequestTimeout(reqTimeout) | |
.build(); | |
httpClient = HttpClients.custom() | |
.setConnectionManager(priClientPool) | |
.setConnectionManagerShared(true) | |
.setDefaultRequestConfig(requestConfig).build(); | |
return httpClient; | |
} | |
公有: | |
首先要在static静态池中创建一个PoolingHttpClientConnectionManager | |
private static PoolingHttpClientConnectionManager pubClientPool =null; | |
并在class加载时新建: | |
static{ | |
String MAX_CLIENT = Configuration.getInstance().getValue("MAX_CLIENT"); | |
Integer maxClient = Integer.parseInt(MAX_CLIENT); | |
LayeredConnectionSocketFactory sslsf = null; | |
try { | |
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault()); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create() | |
.register("https", sslsf) | |
.register("http", new PlainConnectionSocketFactory()) | |
.build(); | |
pubClientPool = new PoolingHttpClientConnectionManager(socketFactoryRegistry); | |
pubClientPool.setMaxTotal(maxClient); | |
pubClientPool.setDefaultMaxPerRoute(maxClient); | |
} | |
/** | |
* @author: WJJ | |
* @date: 2017年11月24日 上午9:55:31 | |
* @Description: TODO(调用公用连接池,返回一个连接) | |
* @param soTimeout | |
* @param reqTimeout | |
* @param connectionTimeout | |
* @return | |
*/ | |
public static HttpClient getPubHttpClient(int soTimeout,int reqTimeout,int connectionTimeout){ | |
HttpClient httpClient =null; | |
try{ | |
RequestConfig requestConfig =RequestConfig.custom() | |
.setSocketTimeout(10000) | |
.setConnectTimeout(10000) | |
.setConnectionRequestTimeout(15000) | |
.build(); | |
httpClient = HttpClients.custom() | |
.setConnectionManager(pubClientPool) | |
.setConnectionManagerShared(true) | |
.setDefaultRequestConfig(requestConfig).build(); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return httpClient; | |
} | |
/** | |
* @author: WJJ | |
* @date: 2017年11月24日 上午10:20:02 | |
* @Description: TODO(根据传入来的httpClient发送数据) | |
* @param entity | |
* @param trans_url | |
* @param channlClient | |
* @return | |
* @throws Exception | |
*/ | |
public static String doPostByClient(HttpEntity entity, String trans_url,HttpClient channlClient) throws Exception { | |
HttpPost postMethod = new HttpPost(trans_url); | |
try { | |
postMethod.setEntity(entity); | |
HttpResponse resp = channlClient.execute(postMethod); | |
InputStream inResp=resp.getEntity().getContent(); | |
String str = IOUtils.toString(inResp, URL_PARAM_DECODECHARSET_UTF8); | |
inResp.close(); | |
int statusCode = resp.getStatusLine().getStatusCode(); | |
if (200 == statusCode) { | |
return str; | |
} else { | |
logger.info("返回错误码:" + statusCode); | |
throw new Exception("通讯错误" + String.valueOf(statusCode)); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw e; | |
} finally { | |
if (postMethod != null) { | |
postMethod.releaseConnection(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment