做支付遇到的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完成了给买家发送邮件的功能,还遗留一个功能,就 ...
随机推荐
- Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- WINDOWS XP 系统显示乱码的解决方法(修改注册表,使用正常字体)
一位同事的计算机进入WINDOWS XP系统后,电脑里的所有汉字全部显示乱码,很多办公文档无法打开而影响工作.因为第一次遇到这种问题,当然首先是百度解决了,搜索了相关的信息后找到了答案 ...
- Getting started with ASP.NET Core MVC and Visual Studio
This tutorial will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio ...
- 大数据DDos检测——DDos攻击本质上是时间序列数据,t+1时刻的数据特点和t时刻强相关,因此用HMM或者CRF来做检测是必然! 和一个句子的分词算法CRF没有区别!
DDos攻击本质上是时间序列数据,t+1时刻的数据特点和t时刻强相关,因此用HMM或者CRF来做检测是必然!——和一个句子的分词算法CRF没有区别!注:传统DDos检测直接基于IP数据发送流量来识别, ...
- 负载均衡获得真实源IP的6种方法
除了X-FORWARD-FOR,负载均衡中获得真实源IP的方法还有很多种. 本文抛砖引玉,主要介绍获得真实源IP的多种方法,而不是具体配置. 负载均衡获得真实IP的方法有很多种,将形成专题文章. 本文 ...
- Meson使用
安装 Meson是基于python3实现,至少需要python3.5才能运行,默认采用ninja作为后端.在Ubuntu下最简单的是通过pip3安装 $ sudo apt-get install py ...
- 大数据查询——HBase读写设计与实践--转
背景介绍 本项目主要解决 check 和 opinion2 张历史数据表(历史数据是指当业务发生过程中的完整中间流程和结果数据)的在线查询.原实现基于 Oracle 提供存储查询服务,随着数据量的不断 ...
- MD5三种方法的学习总结
MD5百度百科 MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已 ...
- Several Ideas on Perl List Context
According to Beginning Perl Book published by Tsinghua Pub., the list context appears when you are t ...
- 基于jQuery封装一个瀑布流插件
/*封装一个瀑布流插件*/ (function($){ $.fn.WaterFall = function(){ /*这是你初始化 调用这个方法的时候的 那个jquery选着到的dom对象 this* ...