做支付遇到的HttpClient大坑
前言
缺陷复现
public class JettyServerMain {
public static void main(String[] args) throws Exception {
Server server = new Server(8888); server.setHandler(new HelloHandler()); server.start();
server.join();
}
} class HelloHandler extends AbstractHandler { /**
* 作为测试,在这个方法故意sleep 3秒,然后返回hello;
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
long threadId = Thread.currentThread().getId();
Log.getLogger(this.getClass()).info("threadId="+threadId+" come in");
try {
Thread.sleep(3000);
}
catch(Exception e) {
e.printStackTrace();
} response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter(); out.println("hello+"+threadId); baseRequest.setHandled(true);
Log.getLogger(this.getClass()).info("threadId="+threadId+" finish");
}
}
public class HTTPClientV1 {
public static void main(String argvs[]){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Get请求
HttpGet httpGet = new HttpGet("http://localhost:8888");
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) {
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String argvs[]){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
for(int i=0;i<10;i++) {
new Thread(new Runnable() {
@Override
public void run() {
GetRequest(httpClient);
}
}).start();
}
}
22:48:46.618 INFO log: Logging initialized @897ms
22:48:46.655 INFO Server: jetty-9.2.14.v20151106
22:48:47.051 INFO ServerConnector: Started ServerConnector@5136ac92{HTTP/1.1}{0.0.0.0:8888}
22:48:47.052 INFO Server: Started @1346ms
22:49:59.056 INFO HelloHandler: threadId=15 come in
22:49:59.057 INFO HelloHandler: threadId=14 come in
22:50:02.080 INFO HelloHandler: threadId=14 finish
22:50:02.080 INFO HelloHandler: threadId=15 finish
22:50:02.144 INFO HelloHandler: threadId=15 come in
22:50:02.144 INFO HelloHandler: threadId=19 come in
22:50:05.144 INFO HelloHandler: threadId=19 finish
22:50:05.144 INFO HelloHandler: threadId=15 finish
22:50:05.148 INFO HelloHandler: threadId=19 come in
22:50:05.148 INFO HelloHandler: threadId=14 come in
22:50:08.149 INFO HelloHandler: threadId=19 finish
22:50:08.149 INFO HelloHandler: threadId=14 finish
22:50:08.153 INFO HelloHandler: threadId=15 come in
22:50:08.153 INFO HelloHandler: threadId=19 come in
22:50:11.153 INFO HelloHandler: threadId=19 finish
22:50:11.153 INFO HelloHandler: threadId=15 finish
22:50:11.158 INFO HelloHandler: threadId=14 come in
22:50:11.158 INFO HelloHandler: threadId=19 come in
22:50:14.158 INFO HelloHandler: threadId=19 finish
22:50:14.158 INFO HelloHandler: threadId=14 finish
// 创建Get请求
HttpGet httpGet = new HttpGet("http://localhost:8888");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000)
.setConnectTimeout(2000)
.build();
httpGet.setConfig(requestConfig);
22:55:32.751 INFO HelloHandler: threadId=15 come in
22:55:32.751 INFO HelloHandler: threadId=14 come in
22:55:34.758 INFO HelloHandler: threadId=19 come in
22:55:34.759 INFO HelloHandler: threadId=21 come in
22:55:35.751 INFO HelloHandler: threadId=15 finish
22:55:35.751 INFO HelloHandler: threadId=14 finish
22:55:36.761 INFO HelloHandler: threadId=23 come in
22:55:36.767 INFO HelloHandler: threadId=14 come in
22:55:37.760 INFO HelloHandler: threadId=19 finish
22:55:37.761 INFO HelloHandler: threadId=21 finish
22:55:38.764 INFO HelloHandler: threadId=15 come in
22:55:38.769 INFO HelloHandler: threadId=19 come in
22:55:39.761 INFO HelloHandler: threadId=23 finish
22:55:39.767 INFO HelloHandler: threadId=14 finish
22:55:40.766 INFO HelloHandler: threadId=21 come in
22:55:40.771 INFO HelloHandler: threadId=23 come in
22:55:41.764 INFO HelloHandler: threadId=15 finish
22:55:41.770 INFO HelloHandler: threadId=19 finish
22:55:43.766 INFO HelloHandler: threadId=21 finish
22:55:43.771 INFO HelloHandler: threadId=23 finish
import java.io.IOException; import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils; /**
* Date: 2019/5/22
* TIME: 21:25
* HTTPClient
* 1、共享httpclient
* 2、增加超时时间
* @author donlianli
*/
public class HTTPClientV3 {
public static void main(String argvs[]){
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); for(int i=0;i<10;i++) {
new Thread(new Runnable() {
@Override
public void run() {
GetRequest(httpClient);
}
}).start();
}
} private static void GetRequest(CloseableHttpClient httpClient) {
// 创建Get请求
HttpGet httpGet = new HttpGet("http://localhost:8888");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000)
.setConnectTimeout(2000)
.build();
httpGet.setConfig(requestConfig);
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) {
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
}
public static void main(String argvs[]){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 总连接数
cm.setMaxTotal(200);
// 这个至少要大于10
cm.setDefaultMaxPerRoute(20);
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(cm).build(); for(int i=0;i<10;i++) {
new Thread(new Runnable() {
@Override
public void run() {
GetRequest(httpClient);
}
}).start();
}
}
扩展延伸
做支付遇到的HttpClient大坑的更多相关文章
- 使用 ping++做支付的流程
获取支付凭据 /// <summary> /// 获取支付凭据 /// </summary> /// <param name="model">& ...
- 干货 | 高耦合场景下,Trip.com如何做支付设计与落地
干货 | 高耦合场景下,Trip.com如何做支付设计与落地 https://mp.weixin.qq.com/s/VR9NTR3RpKVfmUPcwgMABg 原创 Ryann Liu 携程技术 2 ...
- HttpClient学习(四)—— 关于Http
一.Http状态码 状态码分类 100 ~ 199 信息提示 200 ~ 299 成功 300 ~ 399 重定向 400 ~ 499 客户端错误 500 ~ 599 服务端错误 常见状态码 200 ...
- 微信支付开发h5调用
这两天做微信支付开发.碰到大坑.纠结死我了.好不容做完. 后台java:直接上代码:注意区分前后端的变量大小写... @RequestMapping(value = "/index" ...
- APP支付报错ALI40247处理方案!
简直日狗!这里要吐槽支付宝: 1.支付宝文档太复杂,分类虽然详细,但是我找不到app支付 对应服务端的demo 2.提供下载的sdk都是全整合的 用下来都是一条龙服务,还有一些客户端(app)的请求也 ...
- Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)
Phonegap项目,做支付的时候,当把网站打包到ios或android端成app后,在app上通过wap调用银联在线存在一个问题: 就是当从银联支付成功后,再从服务器返回到app客户端就很难实现. ...
- JAVA微信支付接口开发——支付
微信支付接口开发--支付 这几天在做支付服务,系统接入了支付宝.微信.银联三方支付接口.个人感觉支付宝的接口开发较为简单,并且易于测试. 关于数据传输,微信是用xml,所以需要对xml进行解析. 1. ...
- 微信 JS API 支付教程
最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了. 用 ...
- 【SSH网上商城项目实战26】完成订单支付后的短信发送功能
转自: https://blog.csdn.net/eson_15/article/details/51475431 上一节我们使用了Java mail完成了给买家发送邮件的功能,还遗留一个功能,就 ...
随机推荐
- 【bzoj1001】【狼抓兔子】
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 12719 Solved: 3017 [Submit][ ...
- Cocos2d-x 3.0final 终结者系列教程13-贪食蛇游戏案例(全)
快过节了.谢谢了屈原,我们爱你. 应该多几个向屈大人一样跳江的,这样我们就能够放假纪念啦. ---------------------------------快过节了.弄个案例,大家最好还是假期做做, ...
- 基础树形DP小结
HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...
- scanf,printf函数细节
今天笔试的时候遇到一个考察C语言scanf函数的题目 int x; float y; scanf("%3d%f",&x,&y); // input 123456 6 ...
- VIM中括号的自动补全与删除
先放来源 http://oldj.net/article/vim-parenthesis/ 很多现代 IDE 都有自动补全配对括号的功能,比如输入了左括号“(”,IDE 就自动在后面添加一个对应的右括 ...
- Controller methods and views
https://docs.asp.net/en/latest/tutorials/first-mvc-app/controller-methods-views.html We have a good ...
- raspberry-常用命令
安全关闭raspberry:sudo shutdown -h now 一次升级系统中的所有内容:sudo apt-get update 升级单个软件包:sudo apt-get install *** ...
- [CF1139 E] Maximize Mex 解题报告 (二分图匹配)
interlinkage: https://codeforces.com/contest/1139/problem/E description: 有$n$个学生,$m$个社团,每个学生有一个能力值,属 ...
- python基本数据类型之列表list
list的基本功能 结果类型 中括号括起来 逗号(,)分割每一个元素 列表中的元素可以是数字,字符串,列表,布尔值所有的都能放 索引,切片 ? 1 2 3 li = [1, 3, 5, " ...
- B - Mike and Cellphone(map)
Problem description While swimming at the beach, Mike has accidentally dropped his cellphone into th ...