From 4c05d55629ca496c1d0fb6449191f5bcb09704a6 Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Sun, 30 Sep 2018 18:01:22 +0800 Subject: [PATCH 1/6] =?UTF-8?q?1.=E5=8E=BB=E6=8E=89MoreValidate=20?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=B8=BAValidate=20=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E9=80=89=E6=8B=A9=E6=80=A7=202.=E4=BF=AE=E6=94=B9Bean?= =?UTF-8?q?UtilTest=20=E5=AF=B9=E5=BA=94=E7=9A=84=E7=B1=BB=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E5=AE=9E=E9=99=85=E4=B8=8A=E8=BF=99=E4=B8=AA=E7=B1=BB?= =?UTF-8?q?=E6=98=AFBooleanUtil=E7=9A=84=E6=B5=8B=E8=AF=95=E7=B1=BB=203.?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E9=87=8D=E8=BD=BD=E8=A7=84?= =?UTF-8?q?=E8=8C=83=EF=BC=8C=E9=87=8D=E8=BD=BD=E7=BB=9F=E4=B8=80=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A@overwire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/{MoreValidate.java => Validate.java} | 4 +- .../collection/type/ConcurrentHashSet.java | 14 ++++ .../vip/vjtools/vjkit/number/RandomUtil.java | 9 ++- ...BeanUtilTest.java => BooleanUtilTest.java} | 9 ++- ...oreValidateTest.java => ValidateTest.java} | 68 +++++++++---------- .../threadpool/ThreadPoolBuilderTest.java | 24 +++---- 6 files changed, 69 insertions(+), 59 deletions(-) rename vjkit/src/main/java/com/vip/vjtools/vjkit/base/{MoreValidate.java => Validate.java} (97%) rename vjkit/src/test/java/com/vip/vjtools/vjkit/base/{BeanUtilTest.java => BooleanUtilTest.java} (89%) rename vjkit/src/test/java/com/vip/vjtools/vjkit/base/{MoreValidateTest.java => ValidateTest.java} (66%) diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/base/MoreValidate.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/base/Validate.java similarity index 97% rename from vjkit/src/main/java/com/vip/vjtools/vjkit/base/MoreValidate.java rename to vjkit/src/main/java/com/vip/vjtools/vjkit/base/Validate.java index b7670b8a..bf478b4a 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/base/MoreValidate.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/base/Validate.java @@ -8,9 +8,9 @@ * 为什么不用Guava的{@code com.google.common.base.Preconditions} , 一是少打几个字而已, 二是Validate的方法多,比如noNullElements()判断多个元素都不为空 * * 目前主要参考 {@code com.google.common.math.MathPreconditions} , 补充数字为正数或非负数的校验 - * + * */ -public class MoreValidate { +public class Validate extends org.apache.commons.lang3.Validate { /** * 校验为正数则返回该数字,否则抛出异常. diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/collection/type/ConcurrentHashSet.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/collection/type/ConcurrentHashSet.java index 10e7e9ef..975e1e98 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/collection/type/ConcurrentHashSet.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/collection/type/ConcurrentHashSet.java @@ -27,38 +27,47 @@ public ConcurrentHashSet() { s = m.keySet(); } + @Override public void clear() { m.clear(); } + @Override public int size() { return m.size(); } + @Override public boolean isEmpty() { return m.isEmpty(); } + @Override public boolean contains(Object o) { return m.containsKey(o); } + @Override public boolean remove(Object o) { return m.remove(o) != null; } + @Override public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; } + @Override public Iterator iterator() { return s.iterator(); } + @Override public Object[] toArray() { return s.toArray(); } + @Override public T[] toArray(T[] a) { return s.toArray(a); } @@ -68,22 +77,27 @@ public String toString() { return s.toString(); } + @Override public int hashCode() { return s.hashCode(); } + @Override public boolean equals(Object o) { return o == this || s.equals(o); } + @Override public boolean containsAll(Collection c) { return s.containsAll(c); } + @Override public boolean removeAll(Collection c) { return s.removeAll(c); } + @Override public boolean retainAll(Collection c) { return s.retainAll(c); } diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/number/RandomUtil.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/number/RandomUtil.java index 73d8ab1b..82621eba 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/number/RandomUtil.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/number/RandomUtil.java @@ -6,9 +6,8 @@ import java.util.concurrent.ThreadLocalRandom; import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.Validate; -import com.vip.vjtools.vjkit.base.MoreValidate; +import com.vip.vjtools.vjkit.base.Validate; /** * 随机数工具集. @@ -104,7 +103,7 @@ public static int nextInt(int min, int max) { */ public static int nextInt(Random random, int min, int max) { Validate.isTrue(max >= min, "Start value must be smaller or equal to end value."); - MoreValidate.nonNegative("min", min); + Validate.nonNegative("min", min); if (min == max) { return min; @@ -168,7 +167,7 @@ public static long nextLong(long min, long max) { */ public static long nextLong(Random random, long min, long max) { Validate.isTrue(max >= min, "Start value must be smaller or equal to end value."); - MoreValidate.nonNegative("min", min); + Validate.nonNegative("min", min); if (min == max) { return min; @@ -220,7 +219,7 @@ public static double nextDouble(final double min, final double max) { */ public static double nextDouble(Random random, final double min, final double max) { Validate.isTrue(max >= min, "Start value must be smaller or equal to end value."); - MoreValidate.nonNegative("min", min); + Validate.nonNegative("min", min); if (Double.compare(min, max) == 0) { return min; diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/base/BeanUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/base/BooleanUtilTest.java similarity index 89% rename from vjkit/src/test/java/com/vip/vjtools/vjkit/base/BeanUtilTest.java rename to vjkit/src/test/java/com/vip/vjtools/vjkit/base/BooleanUtilTest.java index 7fa5dd16..f3cd612e 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/base/BeanUtilTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/base/BooleanUtilTest.java @@ -1,10 +1,13 @@ package com.vip.vjtools.vjkit.base; -import static org.assertj.core.api.Assertions.*; - import org.junit.Test; -public class BeanUtilTest { +import static org.assertj.core.api.Assertions.assertThat; + +/** + * 优先使用org.apache.commons.lang3.BooleanUtils + */ +public class BooleanUtilTest { @Test public void test() { diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/base/MoreValidateTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/base/ValidateTest.java similarity index 66% rename from vjkit/src/test/java/com/vip/vjtools/vjkit/base/MoreValidateTest.java rename to vjkit/src/test/java/com/vip/vjtools/vjkit/base/ValidateTest.java index e4226507..8e2f424e 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/base/MoreValidateTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/base/ValidateTest.java @@ -1,64 +1,64 @@ package com.vip.vjtools.vjkit.base; +import org.junit.Test; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; -import org.junit.Test; - -public class MoreValidateTest { +public class ValidateTest { @Test public void test() { // int - int a = MoreValidate.nonNegative("x", 0); + int a = Validate.nonNegative("x", 0); assertThat(a).isEqualTo(0); - a = MoreValidate.nonNegative("x", 1); + a = Validate.nonNegative("x", 1); assertThat(a).isEqualTo(1); - a = MoreValidate.positive("x", 1); + a = Validate.positive("x", 1); assertThat(a).isEqualTo(1); // Integer - Integer c = MoreValidate.nonNegative("x", Integer.valueOf(0)); + Integer c = Validate.nonNegative("x", Integer.valueOf(0)); assertThat(c).isEqualTo(0); - c = MoreValidate.nonNegative("x", Integer.valueOf(21)); + c = Validate.nonNegative("x", Integer.valueOf(21)); assertThat(c).isEqualTo(21); - c = MoreValidate.positive("x", Integer.valueOf(1)); + c = Validate.positive("x", Integer.valueOf(1)); assertThat(c).isEqualTo(1); // long - long b = MoreValidate.nonNegative("x", 0l); + long b = Validate.nonNegative("x", 0l); assertThat(b).isEqualTo(0); - b = MoreValidate.nonNegative("x", 11l); + b = Validate.nonNegative("x", 11l); assertThat(b).isEqualTo(11); - b = MoreValidate.positive("x", 1l); + b = Validate.positive("x", 1l); assertThat(b).isEqualTo(1); - double e = MoreValidate.nonNegative("x", 0l); + double e = Validate.nonNegative("x", 0l); assertThat(e).isEqualTo(0); - e = MoreValidate.nonNegative("x", 11d); + e = Validate.nonNegative("x", 11d); assertThat(e).isEqualTo(11); - e = MoreValidate.positive("x", 1.1d); + e = Validate.positive("x", 1.1d); assertThat(e).isEqualTo(1.1); // Long - Long d = MoreValidate.nonNegative("x", Long.valueOf(0)); + Long d = Validate.nonNegative("x", Long.valueOf(0)); assertThat(d).isEqualTo(0); - d = MoreValidate.positive("x", Long.valueOf(1)); + d = Validate.positive("x", Long.valueOf(1)); assertThat(d).isEqualTo(1); - d = MoreValidate.nonNegative("x", Long.valueOf(11)); + d = Validate.nonNegative("x", Long.valueOf(11)); assertThat(d).isEqualTo(11); // int try { - MoreValidate.nonNegative("x", -1); + Validate.nonNegative("x", -1); fail("fail"); } catch (Throwable t) { assertThat(t).hasMessage("x (-1) must be >= 0"); @@ -66,7 +66,7 @@ public void test() { } try { - MoreValidate.nonNegative(null, -1); + Validate.nonNegative(null, -1); fail("fail"); } catch (Throwable t) { assertThat(t).hasMessage("null (-1) must be >= 0"); @@ -75,14 +75,14 @@ public void test() { } try { - MoreValidate.positive("x", -1); + Validate.positive("x", -1); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", 0); + Validate.positive("x", 0); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); @@ -90,20 +90,20 @@ public void test() { // long try { - MoreValidate.nonNegative("x", -1l); + Validate.nonNegative("x", -1l); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", -1l); + Validate.positive("x", -1l); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", 0l); + Validate.positive("x", 0l); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); @@ -111,20 +111,20 @@ public void test() { // Long try { - MoreValidate.nonNegative("x", Long.valueOf(-1)); + Validate.nonNegative("x", Long.valueOf(-1)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", Long.valueOf(-1)); + Validate.positive("x", Long.valueOf(-1)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", Long.valueOf(0)); + Validate.positive("x", Long.valueOf(0)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); @@ -132,21 +132,21 @@ public void test() { // Integer try { - MoreValidate.nonNegative("x", Integer.valueOf(-1)); + Validate.nonNegative("x", Integer.valueOf(-1)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", Integer.valueOf(-1)); + Validate.positive("x", Integer.valueOf(-1)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", Integer.valueOf(0)); + Validate.positive("x", Integer.valueOf(0)); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); @@ -154,20 +154,20 @@ public void test() { // double try { - MoreValidate.nonNegative("x", -9999.2d); + Validate.nonNegative("x", -9999.2d); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", -1.2d); + Validate.positive("x", -1.2d); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); } try { - MoreValidate.positive("x", 0d); + Validate.positive("x", 0d); fail("fail"); } catch (Throwable t) { assertThat(t).isInstanceOf(IllegalArgumentException.class); diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/concurrent/threadpool/ThreadPoolBuilderTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/concurrent/threadpool/ThreadPoolBuilderTest.java index ffc7b316..8f431c61 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/concurrent/threadpool/ThreadPoolBuilderTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/concurrent/threadpool/ThreadPoolBuilderTest.java @@ -1,17 +1,11 @@ package com.vip.vjtools.vjkit.concurrent.threadpool; -import static org.assertj.core.api.Assertions.*; - -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.SynchronousQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - +import com.vip.vjtools.vjkit.concurrent.threadpool.QueuableCachedThreadPool.ControllableQueue; import org.junit.Test; -import com.vip.vjtools.vjkit.concurrent.threadpool.QueuableCachedThreadPool.ControllableQueue; +import java.util.concurrent.*; + +import static org.assertj.core.api.Assertions.assertThat; public class ThreadPoolBuilderTest { @@ -124,12 +118,12 @@ public void quequablePool() { assertThat(singlePool.getQueue()).isInstanceOf(ControllableQueue.class); singlePool.shutdown(); - ThreadPoolExecutor sizeablePool = ThreadPoolBuilder.queuableCachedPool().setMinSize(10).setMaxSize(100) + ThreadPoolExecutor sizeAblePool = ThreadPoolBuilder.queuableCachedPool().setMinSize(10).setMaxSize(100) .setKeepAliveSecs(20).build(); - assertThat(sizeablePool.getCorePoolSize()).isEqualTo(10); - assertThat(sizeablePool.getMaximumPoolSize()).isEqualTo(100); - assertThat(sizeablePool.getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(20); - sizeablePool.shutdown(); + assertThat(sizeAblePool.getCorePoolSize()).isEqualTo(10); + assertThat(sizeAblePool.getMaximumPoolSize()).isEqualTo(100); + assertThat(sizeAblePool.getKeepAliveTime(TimeUnit.SECONDS)).isEqualTo(20); + sizeAblePool.shutdown(); ThreadPoolExecutor fixPoolWithNamePrefix = ThreadPoolBuilder.queuableCachedPool() .setThreadNamePrefix("queuableCachedPool").build(); From bcfa5be3aa43cade6f93cbead97c609272ada4e6 Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Fri, 5 Oct 2018 22:21:51 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0HttpClientUtil?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vjkit/pom.xml | 16 + .../vjtools/vjkit/net/HttpClientResult.java | 59 +++ .../vip/vjtools/vjkit/net/HttpClientUtil.java | 398 ++++++++++++++++++ .../vjtools/vjkit/mapper/BeanMapperTest.java | 27 +- .../vjtools/vjkit/net/HttpClientUtilTest.java | 168 ++++++++ 5 files changed, 663 insertions(+), 5 deletions(-) create mode 100644 vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java create mode 100644 vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java create mode 100644 vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java diff --git a/vjkit/pom.xml b/vjkit/pom.xml index 5f910e0c..76028787 100644 --- a/vjkit/pom.xml +++ b/vjkit/pom.xml @@ -18,6 +18,8 @@ 4.12 2.6.0 2.18.3 + 4.5.6 + UTF-8 @@ -41,6 +43,20 @@ ${commons-lang3.version} + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + + org.apache.httpcomponents + fluent-hc + ${httpclient.version} + + + org.slf4j diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java new file mode 100644 index 00000000..bf075e22 --- /dev/null +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java @@ -0,0 +1,59 @@ +package com.vip.vjtools.vjkit.net; + +import java.io.Serializable; + +/** + * 封装httpClient响应结果 + */ +public class HttpClientResult implements Serializable { + + private static final long serialVersionUID = 2168152194164783950L; + + /** + * 响应状态码 + */ + private int code; + + /** + * 响应数据 + */ + private String content; + + public HttpClientResult() { + } + + public HttpClientResult(int code) { + this.code = code; + } + + public HttpClientResult(String content) { + this.content = content; + } + + public HttpClientResult(int code, String content) { + this.code = code; + this.content = content; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "HttpClientResult [code=" + code + ", content=" + content + "]"; + } + +} diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java new file mode 100644 index 00000000..1b8e9e58 --- /dev/null +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java @@ -0,0 +1,398 @@ +package com.vip.vjtools.vjkit.net; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.LayeredConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +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.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.*; +import java.util.Map.Entry; + +/** + * httpClient工具类 + * + * @author lost + */ +public class HttpClientUtil { + + /** + * 编码格式。发送编码格式统一用UTF-8 + */ + private static final String ENCODING = "UTF-8"; + + /** + * 设置连接超时时间,单位毫秒。 + */ + private static final int CONNECT_TIMEOUT = 6000; + + /** + * 请求获取数据的超时时间(即响应时间),单位毫秒。 + */ + private static final int SOCKET_TIMEOUT = 6000; + + private static PoolingHttpClientConnectionManager connectionManager; + + private static boolean sharePool; + + public static void sharePool(boolean flag) { + sharePool = flag; + if (connectionManager == null && flag){ + init(); + } else { + connectionManager = null; + } + } + + public static void init() { + ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); + LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory(); + Registry registry = RegistryBuilder.create() + .register("http", plainsf) + .register("https", sslsf) + .build(); + + connectionManager = new PoolingHttpClientConnectionManager(registry); + // 将最大连接数增加到200   + connectionManager.setMaxTotal(200); + // 将每个路由基础的连接增加到20 + connectionManager.setDefaultMaxPerRoute(20); + } + + public static CloseableHttpClient getHttpClient() { + CloseableHttpClient httpClient; + if (sharePool == true){ + httpClient = HttpClients.custom() + .setConnectionManager(connectionManager).setConnectionManagerShared(true) + .build(); + } else { + httpClient = HttpClients.createDefault(); + } + + return httpClient; + } + + /** + * 发送get请求;不带请求头和请求参数 + * + * @param url 请求地址 + * @return + * @throws Exception + */ + public static HttpClientResult doGet(String url) throws Exception { + return doGet(url, null, null); + } + + /** + * 发送get请求;带请求参数 + * + * @param url 请求地址 + * @param params 请求参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doGet(String url, Map params) throws Exception { + return doGet(url, null, params); + } + + /** + * 发送get请求;带请求头和请求参数 + * + * @param url 请求地址 + * @param headers 请求头集合 + * @param params 请求参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doGet(String url, Map headers, Map params) throws + Exception { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建访问的地址 + URIBuilder uriBuilder = new URIBuilder(url); + if (params != null) { + Set> entrySet = params.entrySet(); + for (Entry entry : entrySet) { + uriBuilder.setParameter(entry.getKey(), entry.getValue()); + } + } + + // 创建http对象 + HttpGet httpGet = new HttpGet(uriBuilder.build()); + /** + * setConnectTimeout:设置连接超时时间,单位毫秒。 + * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection + * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 + * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 + */ + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( + SOCKET_TIMEOUT).build(); + httpGet.setConfig(requestConfig); + + // 设置请求头 + packageHeader(headers, httpGet); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + try { + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpGet); + } finally { + // 释放资源 + release(httpResponse, httpClient); + } + } + + /** + * 发送post请求;不带请求头和请求参数 + * + * @param url 请求地址 + * @return + * @throws Exception + */ + public static HttpClientResult doPost(String url) throws Exception { + return doPost(url, null, null); + } + + /** + * 发送post请求;带请求参数 + * + * @param url 请求地址 + * @param params 参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doPost(String url, Map params) throws Exception { + return doPost(url, null, params); + } + + /** + * 发送post请求;带请求头和请求参数 + * + * @param url 请求地址 + * @param headers 请求头集合 + * @param params 请求参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doPost(String url, Map headers, Map params) throws + Exception { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + /** + * setConnectTimeout:设置连接超时时间,单位毫秒。 + * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection + * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 + * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 + */ + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( + SOCKET_TIMEOUT).build(); + httpPost.setConfig(requestConfig); + // 设置请求头 + /*httpPost.setHeader("Cookie", ""); + httpPost.setHeader("Connection", "keep-alive"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9"); + httpPost.setHeader("Accept-Encoding", "gzip, deflate, br"); + httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) + Chrome/65.0.3325.181 Safari/537.36");*/ + packageHeader(headers, httpPost); + + // 封装请求参数 + packageParam(params, httpPost); + + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + try { + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpPost); + } finally { + // 释放资源 + release(httpResponse, httpClient); + } + } + + /** + * 发送put请求;不带请求参数 + * + * @param url 请求地址 + * @return + * @throws Exception + */ + public static HttpClientResult doPut(String url) throws Exception { + return doPut(url, null); + } + + /** + * 发送put请求;带请求参数 + * + * @param url 请求地址 + * @param params 参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doPut(String url, Map params) throws Exception { + CloseableHttpClient httpClient = getHttpClient(); + HttpPut httpPut = new HttpPut(url); + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( + SOCKET_TIMEOUT).build(); + httpPut.setConfig(requestConfig); + + packageParam(params, httpPut); + + CloseableHttpResponse httpResponse = null; + + try { + return getHttpClientResult(httpResponse, httpClient, httpPut); + } finally { + release(httpResponse, httpClient); + } + } + + /** + * 发送delete请求;不带请求参数 + * + * @param url 请求地址 + * @return + * @throws Exception + */ + public static HttpClientResult doDelete(String url) throws Exception { + CloseableHttpClient httpClient = getHttpClient(); + HttpDelete httpDelete = new HttpDelete(url); + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( + SOCKET_TIMEOUT).build(); + httpDelete.setConfig(requestConfig); + + CloseableHttpResponse httpResponse = null; + try { + return getHttpClientResult(httpResponse, httpClient, httpDelete); + } finally { + release(httpResponse, httpClient); + } + } + + /** + * 发送delete请求;带请求参数 + * + * @param url 请求地址 + * @param params 参数集合 + * @return + * @throws Exception + */ + public static HttpClientResult doDelete(String url, Map params) throws Exception { + if (params == null) { + params = new HashMap(); + } + + params.put("_method", "delete"); + return doPost(url, params); + } + + /** + * 封装请求头 + * + * @param params + * @param httpMethod + */ + public static void packageHeader(Map params, HttpRequestBase httpMethod) { + // 封装请求头 + if (params != null) { + Set> entrySet = params.entrySet(); + for (Entry entry : entrySet) { + // 设置到请求头到HttpRequestBase对象中 + httpMethod.setHeader(entry.getKey(), entry.getValue()); + } + } + } + + /** + * 封装请求参数 + * + * @param params + * @param httpMethod + * @throws UnsupportedEncodingException + */ + public static void packageParam(Map params, HttpEntityEnclosingRequestBase httpMethod) + throws UnsupportedEncodingException { + // 封装请求参数 + if (params != null) { + List nvps = new ArrayList(); + Set> entrySet = params.entrySet(); + for (Entry entry : entrySet) { + nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + + // 设置到请求的http对象中 + httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING)); + } + } + + /** + * 获得响应结果 + * + * @param httpResponse + * @param httpClient + * @param httpMethod + * @return + * @throws Exception + */ + public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse, + CloseableHttpClient httpClient, HttpRequestBase httpMethod) + throws Exception { + // 执行请求 + httpResponse = httpClient.execute(httpMethod); + + // 获取返回结果 + if (httpResponse != null && httpResponse.getStatusLine() != null) { + String content = ""; + if (httpResponse.getEntity() != null) { + HttpEntity httpEntity = httpResponse.getEntity(); + content = EntityUtils.toString(httpEntity, ENCODING); + + //try to release + EntityUtils.consume(httpEntity); + } + return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content); + } + return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + /** + * 释放资源 + * + * @param httpResponse + * @param httpClient + * @throws IOException + */ + public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException { + // 释放资源 + if (httpResponse != null) { + httpResponse.close(); + } + if (httpClient != null) { + httpClient.close(); + } + } + +} \ No newline at end of file diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/BeanMapperTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/BeanMapperTest.java index ca773eec..e1a9d835 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/BeanMapperTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/BeanMapperTest.java @@ -1,13 +1,12 @@ package com.vip.vjtools.vjkit.mapper; -import static org.assertj.core.api.Assertions.*; +import com.vip.vjtools.vjkit.collection.ListUtil; +import org.junit.Test; import java.util.List; +import java.util.Map; -import org.junit.Test; - -import com.vip.vjtools.vjkit.collection.ListUtil; -import com.vip.vjtools.vjkit.mapper.BeanMapper; +import static org.assertj.core.api.Assertions.assertThat; public class BeanMapperTest { @@ -59,6 +58,18 @@ public void copyArrayObject() { } + @Test + public void copy2Map(){ + Teacher teacher = new Teacher("zhang"); + Map map = BeanMapper.map(teacher,Map.class); + assertThat(map).containsKeys("name").containsValues("zhang"); + + Student student = new Student("zhang3", 20, new Teacher("li4"), ListUtil.newArrayList("chinese", "english")); + Map mapStu = BeanMapper.map(student,Map.class); + assertThat(mapStu.containsKey("teacher")); + assertThat(mapStu.get("teacher")).hasFieldOrProperty("name"); + } + public static class Student { public String name; private int age; @@ -131,6 +142,12 @@ public void setName(String name) { this.name = name; } + @Override + public String toString() { + return "Teacher{" + + "name='" + name + '\'' + + '}'; + } } public static class StudentVO { diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java new file mode 100644 index 00000000..af953cb8 --- /dev/null +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java @@ -0,0 +1,168 @@ +package com.vip.vjtools.vjkit.net; + +import com.vip.vjtools.vjkit.io.FileUtil; +import org.apache.http.client.fluent.Request; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + *

@author: lost + *

@date: 2018/10/05 下午4:36 + */ +public class HttpClientUtilTest { + List urlList = new ArrayList(); + @Before + public void init(){ + for (int i = 1 ; i < 30; i++){ + String url = String.format("http://www.infoq.com/cn/articles/%s",i); + urlList.add(url); + } + } + + @Test + public void doGet() throws Exception { + String url = "http://www.baidu.com"; + HttpClientResult result = HttpClientUtil.doGet(url); + //System.out.println(JsonMapper.INSTANCE.toJson(result)); + assertThat(result.getCode()).isEqualTo(200); + System.out.println(result.getContent()); + assertThat(result.getContent()).isNotBlank(); + + } + + @Test + public void testPoolGet() throws Exception { + HttpClientUtil.sharePool(true); + + long start = System.currentTimeMillis(); + runTask(urlList); + long end = System.currentTimeMillis(); + long temp = (end - start); + System.out.println("共享连接池耗时 -> " + temp); + + } + + + @Test + public void testNoPool(){ + HttpClientUtil.sharePool(false); + long start2 = System.currentTimeMillis(); + runTask(urlList); + long end2 = System.currentTimeMillis(); + System.out.println(" 非共享连接池耗时 -> " + (end2 - start2)); + } + + private void runTask(List urlList) { + int pagecount = urlList.size(); + ExecutorService executors = Executors.newFixedThreadPool(pagecount); + + CountDownLatch countDownLatch = new CountDownLatch(pagecount); + try { + for (int i = 0; i < pagecount; i++) { + //启动线程抓取 + executors.execute(new GetRunnable(urlList.get(i), countDownLatch)); + + } + countDownLatch.await(); + + executors.shutdown(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + + System.out.println( + "线程" + Thread.currentThread().getName() + "," + System.currentTimeMillis() + ", 所有线程已完成,开始进入下一步!"); + } + } + + static class GetRunnable implements Runnable { + + private CountDownLatch countDownLatch; + private String url; + + public GetRunnable(String url, CountDownLatch countDownLatch) { + + this.countDownLatch = countDownLatch; + this.url = url; + + } + + @Override + + public void run() { + + try { + + HttpClientUtil.doGet(url); + + } catch (Exception e) { + + e.printStackTrace(); + + } finally { + countDownLatch.countDown(); + } + + } + + } + + /** + * 使用fluent facade API使用户不必处理连接管理和资源释放。 warn::: 一定要设置超时时间,建议普通请求就使用 + * + * @link http://hc.apache.org/httpcomponents-client-4.5.x/fluent-hc/xref-test/index.html + *

+ * timeout 不设置的默认值是多少?好坑啊,有风险,如果忘记设就出大问题了 + * @see com.vip.vjtools.vjkit.net.HttpClientUtil + * @see org.apache.http.client.config.RequestConfig#getConnectTimeout() A timeout value of zero is interpreted as an + * infinite timeout. A negative value is interpreted as undefined (system default). + */ + @Test + public void fluentDoGet() { + long start = System.currentTimeMillis(); + //不设置超时出大问题,谨慎使用,还是使用自封装的工具类吧 + try { + String result = Request.Get("http://google.com").connectTimeout(1000) + .execute().returnContent().asString(Charset.forName("UTF-8")); //不加这个会乱码 + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println(String.format(" spend %s", System.currentTimeMillis() - start)); + + } + + @Test + public void fluentDownload() throws IOException { + File file = FileUtil.createTempFile().toFile(); + try { + String url = "http://imgsrc.baidu.com/imgad/pic/item/f9dcd100baa1cd11ecf55a03b312c8fcc3ce2d55.jpg"; + Request.Get(url).connectTimeout(6000).execute().saveContent(file); + System.out.println(file.getAbsolutePath()); + } finally { + FileUtil.deleteFile(file); + } + + } + + @Test + public void doPost() throws Exception { + String payUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; + HttpClientResult result = HttpClientUtil.doPost(payUrl); + //System.out.println(result); + assertThat(result.getCode()).isEqualTo(200); + } + +} \ No newline at end of file From b830baa8fa8f8d2985f09021256e303a6bb2fd25 Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Sat, 6 Oct 2018 02:24:43 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0OkHttpUtil=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vjkit/pom.xml | 19 ++ .../vip/vjtools/vjkit/net/HttpClientUtil.java | 101 ++++++++-- ...{HttpClientResult.java => HttpResult.java} | 12 +- .../com/vip/vjtools/vjkit/net/OkHttpUtil.java | 176 ++++++++++++++++++ .../vjtools/vjkit/mapper/JsonMapperTest.java | 20 +- .../vjtools/vjkit/net/HttpClientUtilTest.java | 10 +- .../vip/vjtools/vjkit/net/OkHttpUtilTest.java | 129 +++++++++++++ 7 files changed, 437 insertions(+), 30 deletions(-) rename vjkit/src/main/java/com/vip/vjtools/vjkit/net/{HttpClientResult.java => HttpResult.java} (71%) create mode 100644 vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java create mode 100644 vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java diff --git a/vjkit/pom.xml b/vjkit/pom.xml index 76028787..17a02dc1 100644 --- a/vjkit/pom.xml +++ b/vjkit/pom.xml @@ -4,6 +4,18 @@ com.vip.vjtools vjkit 1.0.7-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + jar vjkit VIP's core java libraries @@ -19,6 +31,7 @@ 2.6.0 2.18.3 4.5.6 + 3.11.0 @@ -56,6 +69,12 @@ ${httpclient.version} + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + + diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java index 1b8e9e58..1ee28084 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientUtil.java @@ -13,6 +13,7 @@ import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; @@ -52,7 +53,7 @@ public class HttpClientUtil { public static void sharePool(boolean flag) { sharePool = flag; - if (connectionManager == null && flag){ + if (connectionManager == null && flag) { init(); } else { connectionManager = null; @@ -76,7 +77,7 @@ public static void init() { public static CloseableHttpClient getHttpClient() { CloseableHttpClient httpClient; - if (sharePool == true){ + if (sharePool == true) { httpClient = HttpClients.custom() .setConnectionManager(connectionManager).setConnectionManagerShared(true) .build(); @@ -94,7 +95,7 @@ public static CloseableHttpClient getHttpClient() { * @return * @throws Exception */ - public static HttpClientResult doGet(String url) throws Exception { + public static HttpResult doGet(String url) throws Exception { return doGet(url, null, null); } @@ -106,7 +107,7 @@ public static HttpClientResult doGet(String url) throws Exception { * @return * @throws Exception */ - public static HttpClientResult doGet(String url, Map params) throws Exception { + public static HttpResult doGet(String url, Map params) throws Exception { return doGet(url, null, params); } @@ -119,7 +120,7 @@ public static HttpClientResult doGet(String url, Map params) thr * @return * @throws Exception */ - public static HttpClientResult doGet(String url, Map headers, Map params) throws + public static HttpResult doGet(String url, Map headers, Map params) throws Exception { // 创建httpClient对象 CloseableHttpClient httpClient = getHttpClient(); @@ -167,7 +168,7 @@ public static HttpClientResult doGet(String url, Map headers, Ma * @return * @throws Exception */ - public static HttpClientResult doPost(String url) throws Exception { + public static HttpResult doPost(String url) throws Exception { return doPost(url, null, null); } @@ -179,10 +180,76 @@ public static HttpClientResult doPost(String url) throws Exception { * @return * @throws Exception */ - public static HttpClientResult doPost(String url, Map params) throws Exception { + public static HttpResult doPost(String url, Map params) throws Exception { return doPost(url, null, params); } + /** + * 发送post请求;json + * + * @param url + * @param json + * @return + * @throws Exception + */ + public static HttpResult doPostJson(String url, String json) throws Exception { + return doPostJson(url, null, json); + } + + /** + * 发送post请求;带请求头和请求参数 + * + * @param url 请求地址 + * @param headers 请求头集合 + * @param json 请求参数json + * @return + * @throws Exception + */ + public static HttpResult doPostJson(String url, Map headers, String json) throws + Exception { + // 创建httpClient对象 + CloseableHttpClient httpClient = getHttpClient(); + + // 创建http对象 + HttpPost httpPost = new HttpPost(url); + /** + * setConnectTimeout:设置连接超时时间,单位毫秒。 + * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection + * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 + * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 + */ + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( + SOCKET_TIMEOUT).build(); + httpPost.setConfig(requestConfig); + // 设置请求头 + /*httpPost.setHeader("Cookie", ""); + httpPost.setHeader("Connection", "keep-alive"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9"); + httpPost.setHeader("Accept-Encoding", "gzip, deflate, br"); + httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) + Chrome/65.0.3325.181 Safari/537.36");*/ + packageHeader(headers, httpPost); + + // 封装请求参数 + json = json == null ? "{}" : json; + StringEntity s = new StringEntity(json); + s.setContentEncoding("UTF-8"); + //发送json数据需要设置contentType + s.setContentType("application/json"); + httpPost.setEntity(s); + // 创建httpResponse对象 + CloseableHttpResponse httpResponse = null; + + try { + // 执行请求并获得响应结果 + return getHttpClientResult(httpResponse, httpClient, httpPost); + } finally { + // 释放资源 + release(httpResponse, httpClient); + } + } + /** * 发送post请求;带请求头和请求参数 * @@ -192,7 +259,7 @@ public static HttpClientResult doPost(String url, Map params) th * @return * @throws Exception */ - public static HttpClientResult doPost(String url, Map headers, Map params) throws + public static HttpResult doPost(String url, Map headers, Map params) throws Exception { // 创建httpClient对象 CloseableHttpClient httpClient = getHttpClient(); @@ -240,7 +307,7 @@ public static HttpClientResult doPost(String url, Map headers, M * @return * @throws Exception */ - public static HttpClientResult doPut(String url) throws Exception { + public static HttpResult doPut(String url) throws Exception { return doPut(url, null); } @@ -252,7 +319,7 @@ public static HttpClientResult doPut(String url) throws Exception { * @return * @throws Exception */ - public static HttpClientResult doPut(String url, Map params) throws Exception { + public static HttpResult doPut(String url, Map params) throws Exception { CloseableHttpClient httpClient = getHttpClient(); HttpPut httpPut = new HttpPut(url); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( @@ -277,7 +344,7 @@ public static HttpClientResult doPut(String url, Map params) thr * @return * @throws Exception */ - public static HttpClientResult doDelete(String url) throws Exception { + public static HttpResult doDelete(String url) throws Exception { CloseableHttpClient httpClient = getHttpClient(); HttpDelete httpDelete = new HttpDelete(url); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout( @@ -300,7 +367,7 @@ public static HttpClientResult doDelete(String url) throws Exception { * @return * @throws Exception */ - public static HttpClientResult doDelete(String url, Map params) throws Exception { + public static HttpResult doDelete(String url, Map params) throws Exception { if (params == null) { params = new HashMap(); } @@ -357,8 +424,8 @@ public static void packageParam(Map params, HttpEntityEnclosingR * @return * @throws Exception */ - public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse, - CloseableHttpClient httpClient, HttpRequestBase httpMethod) + public static HttpResult getHttpClientResult(CloseableHttpResponse httpResponse, + CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception { // 执行请求 httpResponse = httpClient.execute(httpMethod); @@ -367,15 +434,15 @@ public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpRes if (httpResponse != null && httpResponse.getStatusLine() != null) { String content = ""; if (httpResponse.getEntity() != null) { - HttpEntity httpEntity = httpResponse.getEntity(); + HttpEntity httpEntity = httpResponse.getEntity(); content = EntityUtils.toString(httpEntity, ENCODING); //try to release EntityUtils.consume(httpEntity); } - return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content); + return new HttpResult(httpResponse.getStatusLine().getStatusCode(), content); } - return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR); + return new HttpResult(HttpStatus.SC_INTERNAL_SERVER_ERROR); } /** diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpResult.java similarity index 71% rename from vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java rename to vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpResult.java index bf075e22..26f4032e 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpClientResult.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/HttpResult.java @@ -5,7 +5,7 @@ /** * 封装httpClient响应结果 */ -public class HttpClientResult implements Serializable { +public class HttpResult implements Serializable { private static final long serialVersionUID = 2168152194164783950L; @@ -19,18 +19,18 @@ public class HttpClientResult implements Serializable { */ private String content; - public HttpClientResult() { + public HttpResult() { } - public HttpClientResult(int code) { + public HttpResult(int code) { this.code = code; } - public HttpClientResult(String content) { + public HttpResult(String content) { this.content = content; } - public HttpClientResult(int code, String content) { + public HttpResult(int code, String content) { this.code = code; this.content = content; } @@ -53,7 +53,7 @@ public void setContent(String content) { @Override public String toString() { - return "HttpClientResult [code=" + code + ", content=" + content + "]"; + return "HttpResult [code=" + code + ", content=" + content + "]"; } } diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java new file mode 100644 index 00000000..be0b9b9d --- /dev/null +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java @@ -0,0 +1,176 @@ +package com.vip.vjtools.vjkit.net; + +import okhttp3.*; +import org.apache.commons.lang3.Validate; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + *

@author: lost + *

@date: 2018/10/05 下午11:54 + */ +public class OkHttpUtil { + + /** + * 默认连接超时30秒 + */ + public static final int DEFAULT_CONNECTION_TIMEOUT = 30; + + /** + * 默认读超时60秒 + */ + public static final int DEFAULT_READ_TIMEOUT = 60; + + public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + + /** + * 默认client + */ + private static final OkHttpClient client = new OkHttpClient.Builder() + .connectTimeout(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS) + .build(); + + private OkHttpUtil() { + + } + + /** + * get请求 + * + * @param url 请求url + * @return 响应对象 + * @throws IOException io异常 + */ + public static HttpResult doGet(String url) throws IOException { + return doGet(client, url); + } + + /** + * get请求,默认连接超时 + * + * @param url 请求url + * @param readTimeoutInMillis 读超时时间,单位微秒 + * @return 响应对象,需要关闭 + * @throws IOException io异常 + */ + public static HttpResult doGet(String url, int readTimeoutInMillis) throws IOException { + Validate.notEmpty(url, "url can not be null."); + Validate.validState(readTimeoutInMillis >= 0, "timeoutInMillis is illegal."); + + OkHttpClient newClient = new OkHttpClient.Builder() + .connectTimeout(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .readTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS) + .build(); + + return doGet(newClient, url); + } + + /** + * post表单 + * + * @param url 请求地址 + * @param params 表单数据 + * @return 响应对象 + * @throws IOException io异常 + */ + public static HttpResult doPost(String url, Map params) throws IOException { + if (Objects.isNull(params)){ + params = new HashMap<>(); + } + return doPost(client, url, params); + } + + /** + * post表单 + * + * @param url 请求地址 + * @param params 表单数据 + * @param readTimeoutInMillis 读超时时间,单位微秒 + * @return 响应对象 + * @throws IOException io异常 + */ + public static HttpResult doPost(String url, Map params, int readTimeoutInMillis) + throws IOException { + Validate.notEmpty(params, "form body can not be null."); + Validate.validState(readTimeoutInMillis >= 0, "timeoutInMillis is illegal."); + + OkHttpClient newClient = new OkHttpClient.Builder() + .connectTimeout(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .readTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS) + .build(); + + return doPost(newClient, url, params); + } + + /** + * post json + * + * @param url 请求地址 + * @param json json + * @return 返回 + * @throws IOException io异常 + */ + public static HttpResult doPostJson(String url, String json) throws IOException { + return doPostJson(client, url, json); + } + + /** + * post json + * + * @param url 请求地址 + * @param json json + * @param readTimeoutInMillis 读超时,单位微秒 + * @return 响应对象 + * @throws IOException io异常 + */ + public static HttpResult doPostJson(String url, String json, int readTimeoutInMillis) throws IOException { + OkHttpClient newClient = new OkHttpClient.Builder() + .connectTimeout(DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS) + .readTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS) + .build(); + + return doPostJson(newClient, url, json); + } + + private static HttpResult doGet(OkHttpClient httpClient, String url) throws IOException { + Request request = new Request.Builder() + .url(url) + .build(); + + Response response = httpClient.newCall(request).execute(); + return new HttpResult(response.code(), response.body().string()); + } + + private static HttpResult doPost(OkHttpClient httpClient, String url, Map params) + throws IOException { + FormBody.Builder builder = new FormBody.Builder(); + for (Map.Entry entry : params.entrySet()) { + builder.add(entry.getKey(), entry.getValue()); + } + FormBody body = builder.build(); + + Request request = new Request.Builder() + .url(url) + .post(body) + .build(); + + Response response = httpClient.newCall(request).execute(); + return new HttpResult(response.code(), response.body().string()); + } + + private static HttpResult doPostJson(OkHttpClient httpClient, String url, String json) throws IOException { + json = json == null ? "{}" : json; + RequestBody body = RequestBody.create(JSON, json); + Request request = new Request.Builder() + .url(url) + .post(body) + .build(); + Response response = httpClient.newCall(request).execute(); + return new HttpResult(response.code(), response.body().string()); + } +} diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/JsonMapperTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/JsonMapperTest.java index 25d83ead..9a0bf03c 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/JsonMapperTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/mapper/JsonMapperTest.java @@ -1,6 +1,8 @@ package com.vip.vjtools.vjkit.mapper; -import static org.assertj.core.api.Assertions.*; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; @@ -8,10 +10,7 @@ import java.util.Map; import java.util.Map.Entry; -import org.junit.Test; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +import static org.assertj.core.api.Assertions.assertThat; /** * 测试Jackson对Object,Map,List,数组,枚举,日期类等的持久化. 更多测试见showcase中的JsonDemo. @@ -121,6 +120,14 @@ public void nullAndEmpty() { nullBeanResult = JsonMapper.INSTANCE.fromJson("null", TestBean.class); assertThat(nullBeanResult).isNull(); + nullBeanResult = JsonMapper.INSTANCE.fromJson("{}", TestBean.class); + assertThat(nullBeanResult).isNotNull(); + assertThat(nullBeanResult.getDefaultValue()).isEqualTo("hello"); + + + nullBeanResult = JsonMapper.INSTANCE.fromJson("", TestBean.class); + assertThat(nullBeanResult).isNull(); + // Null/Empty String for List List nullListResult = JsonMapper.INSTANCE.fromJson(null, List.class); assertThat(nullListResult).isNull(); @@ -130,6 +137,9 @@ public void nullAndEmpty() { nullListResult = JsonMapper.INSTANCE.fromJson("[]", List.class); assertThat(nullListResult).isEmpty(); + + + } /** diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java index af953cb8..cda2dd1b 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java @@ -33,7 +33,7 @@ public void init(){ @Test public void doGet() throws Exception { String url = "http://www.baidu.com"; - HttpClientResult result = HttpClientUtil.doGet(url); + HttpResult result = HttpClientUtil.doGet(url); //System.out.println(JsonMapper.INSTANCE.toJson(result)); assertThat(result.getCode()).isEqualTo(200); System.out.println(result.getContent()); @@ -160,9 +160,15 @@ public void fluentDownload() throws IOException { @Test public void doPost() throws Exception { String payUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; - HttpClientResult result = HttpClientUtil.doPost(payUrl); + HttpResult result = HttpClientUtil.doPost(payUrl); //System.out.println(result); assertThat(result.getCode()).isEqualTo(200); } + @Test + public void doPostJson() throws Exception { + String apiUrl = "http://127.0.0.1:8080/hello/test"; + System.out.println(HttpClientUtil.doPostJson(apiUrl,null)); + } + } \ No newline at end of file diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java new file mode 100644 index 00000000..c2976aa6 --- /dev/null +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java @@ -0,0 +1,129 @@ +package com.vip.vjtools.vjkit.net; + +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * OkHttp 提供更简洁的api,超时及读超时都有默认值默认的连接池无疑更加优于apache httpclient 建议不要使用apache httpclient + *

@author: lost + *

@date: 2018/10/06 上午1:29 + */ +public class OkHttpUtilTest { + + List urlList = new ArrayList(); + + @Before + public void init() { + for (int i = 1; i < 30; i++) { + String url = String.format("http://www.infoq.com/cn/articles/%s", i); + urlList.add(url); + } + } + + @Test + public void doGet() throws Exception { + String url = "http://www.baidu.com"; + HttpResult result = OkHttpUtil.doGet(url); + //System.out.println(JsonMapper.INSTANCE.toJson(result)); + assertThat(result.getCode()).isEqualTo(200); + System.out.println(result.getContent()); + assertThat(result.getContent()).isNotBlank(); + + } + + @Test + public void doPost() throws IOException { + String payUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; + HttpResult result = OkHttpUtil.doPost(payUrl, null); + System.out.println(result); + assertThat(result.getCode()).isEqualTo(200); + } + + @Test + public void doPostJson() throws IOException { + String apiUrl = "http://127.0.0.1:8080/hello/test"; + //为了和httpClientUtil统一,统一封装成HttpResult + HttpResult httpResult = OkHttpUtil.doPostJson(apiUrl, null); + assertThat(httpResult).isNotNull(); + } + + /** + * OKHttp 有连接池吗?为每个单用户创建一个,这点优于apache httpclient this(5, 5, TimeUnit.MINUTES);高并发下建议,使用一个连接池 简单测试与apache httpclient + * 连接池性能相当 > apache httpclient没有使用连接池 + * + * @see okhttp3.ConnectionPool#ConnectionPool() + */ + @Test + public void testPool() { + long start2 = System.currentTimeMillis(); + runTask(urlList); + long end2 = System.currentTimeMillis(); + System.out.println(" 共享连接池耗时 -> " + (end2 - start2)); + } + + private void runTask(List urlList) { + int pagecount = urlList.size(); + ExecutorService executors = Executors.newFixedThreadPool(pagecount); + + CountDownLatch countDownLatch = new CountDownLatch(pagecount); + try { + for (int i = 0; i < pagecount; i++) { + //启动线程抓取 + executors.execute(new GetRunnable(urlList.get(i), countDownLatch)); + + } + countDownLatch.await(); + + executors.shutdown(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + + System.out.println( + "线程" + Thread.currentThread().getName() + "," + System.currentTimeMillis() + ", 所有线程已完成,开始进入下一步!"); + } + } + + static class GetRunnable implements Runnable { + + private CountDownLatch countDownLatch; + private String url; + + public GetRunnable(String url, CountDownLatch countDownLatch) { + + this.countDownLatch = countDownLatch; + this.url = url; + + } + + @Override + + public void run() { + + try { + + OkHttpUtil.doGet(url); + + } catch (Exception e) { + + e.printStackTrace(); + + } finally { + countDownLatch.countDown(); + } + + } + + } + +} \ No newline at end of file From c6b5e55620afcb599106ab56743e7b71b9ab78e9 Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Sat, 6 Oct 2018 02:34:10 +0800 Subject: [PATCH 4/6] =?UTF-8?q?feat:=E5=8E=BB=E6=8E=89java8=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vjkit/pom.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/vjkit/pom.xml b/vjkit/pom.xml index 17a02dc1..ba9c3563 100644 --- a/vjkit/pom.xml +++ b/vjkit/pom.xml @@ -4,18 +4,6 @@ com.vip.vjtools vjkit 1.0.7-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 8 - 8 - - - - jar vjkit VIP's core java libraries From 4e1da0e456d56374928d14aa2717eec14cce4f42 Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Sat, 6 Oct 2018 02:45:53 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix:=E5=8E=BB=E6=8E=89=E7=9B=B8=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/vip/vjtools/vjkit/net/HttpClientUtilTest.java | 6 ------ .../java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java | 8 -------- 2 files changed, 14 deletions(-) diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java index cda2dd1b..14c36f44 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/HttpClientUtilTest.java @@ -165,10 +165,4 @@ public void doPost() throws Exception { assertThat(result.getCode()).isEqualTo(200); } - @Test - public void doPostJson() throws Exception { - String apiUrl = "http://127.0.0.1:8080/hello/test"; - System.out.println(HttpClientUtil.doPostJson(apiUrl,null)); - } - } \ No newline at end of file diff --git a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java index c2976aa6..ac2b052c 100644 --- a/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java +++ b/vjkit/src/test/java/com/vip/vjtools/vjkit/net/OkHttpUtilTest.java @@ -48,14 +48,6 @@ public void doPost() throws IOException { assertThat(result.getCode()).isEqualTo(200); } - @Test - public void doPostJson() throws IOException { - String apiUrl = "http://127.0.0.1:8080/hello/test"; - //为了和httpClientUtil统一,统一封装成HttpResult - HttpResult httpResult = OkHttpUtil.doPostJson(apiUrl, null); - assertThat(httpResult).isNotNull(); - } - /** * OKHttp 有连接池吗?为每个单用户创建一个,这点优于apache httpclient this(5, 5, TimeUnit.MINUTES);高并发下建议,使用一个连接池 简单测试与apache httpclient * 连接池性能相当 > apache httpclient没有使用连接池 From 6272234fc0a69effecdf302ab92d1a29cabd5fde Mon Sep 17 00:00:00 2001 From: chenhaipeng Date: Sat, 6 Oct 2018 02:59:48 +0800 Subject: [PATCH 6/6] =?UTF-8?q?fix:=E7=89=88=E6=9C=AC=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java index be0b9b9d..9c6e87f8 100644 --- a/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java +++ b/vjkit/src/main/java/com/vip/vjtools/vjkit/net/OkHttpUtil.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.concurrent.TimeUnit; /** @@ -79,7 +78,7 @@ public static HttpResult doGet(String url, int readTimeoutInMillis) throws IOExc * @throws IOException io异常 */ public static HttpResult doPost(String url, Map params) throws IOException { - if (Objects.isNull(params)){ + if (params == null){ params = new HashMap<>(); } return doPost(client, url, params);