Browse Source

修改httputils工具
修改线程池配置

Aladding 6 months ago
parent
commit
7ff6e58055

+ 17 - 8
base-common/src/main/java/com/ozs/common/utils/http/HttpUtils.java

@@ -1,5 +1,6 @@
 package com.ozs.common.utils.http;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -11,14 +12,17 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
 import java.security.cert.X509Certificate;
+
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import com.ozs.common.constant.Constants;
 import com.ozs.common.utils.StringUtils;
 
@@ -64,7 +68,6 @@ public class HttpUtils
      */
     public static String sendGet(String url, String param, String contentType)
     {
-        StringBuilder result = new StringBuilder();
         BufferedReader in = null;
         try
         {
@@ -76,15 +79,21 @@ public class HttpUtils
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
             connection.connect();
-            log.info("connection.getInputStream() - {}", connection.getInputStream());
+            InputStream inputStream = connection.getInputStream();
+            log.info("size {}", inputStream.available());
             log.info("contentType - {}", contentType);
-            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
-            String line;
-            while ((line = in.readLine()) != null)
-            {
-                result.append(line);
+            BufferedInputStream bufIs = new BufferedInputStream(inputStream);
+            
+            byte[] buf = new byte[inputStream.available()];
+            int len = bufIs.read(buf);
+            if (len < 0) {
+            	return "";
             }
+            
+            String result =  new String(buf, contentType);
             log.info("recv - {}", result);
+            
+            return result;
         }
         catch (ConnectException e)
         {
@@ -116,7 +125,7 @@ public class HttpUtils
                 log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
             }
         }
-        return result.toString();
+        return "";
     }
 
     /**

+ 33 - 19
base-framework/src/main/java/com/ozs/framework/config/ThreadPoolConfig.java

@@ -1,13 +1,22 @@
 package com.ozs.framework.config;
 
-import com.ozs.common.utils.Threads;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import javax.annotation.Resource;
+
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.springframework.boot.autoconfigure.task.TaskExecutionProperties;
+import org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Pool;
 import org.springframework.context.annotation.Bean;
+//import com.ozs.common.utils.Threads;
+//import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+//import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor;
+
+import com.ozs.common.utils.Threads;
 
 /**
  * 线程池配置
@@ -17,28 +26,33 @@ import java.util.concurrent.ThreadPoolExecutor;
 @Configuration
 public class ThreadPoolConfig
 {
-    // 核心线程池大小
+	
+	@Resource
+	private TaskExecutionProperties taskExecutionProperties;
+	
+//    // 核心线程池大小
     private int corePoolSize = 50;
-
-    // 最大可创建的线程数
-    private int maxPoolSize = 200;
-
-    // 队列最大长度
-    private int queueCapacity = 1000;
-
-    // 线程池维护线程所允许的空闲时间
-    private int keepAliveSeconds = 300;
+//
+//    // 最大可创建的线程数
+//    private int maxPoolSize = 200;
+//
+//    // 队列最大长度
+//    private int queueCapacity = 1000;
+//
+//    // 线程池维护线程所允许的空闲时间
+//    private int keepAliveSeconds = 300;
 
     @Bean(name = "threadPoolTaskExecutor")
     public ThreadPoolTaskExecutor threadPoolTaskExecutor()
     {
+    	Pool pool = taskExecutionProperties.getPool();
         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
-        executor.setMaxPoolSize(maxPoolSize);
-        executor.setCorePoolSize(corePoolSize);
-        executor.setQueueCapacity(queueCapacity);
-        executor.setKeepAliveSeconds(keepAliveSeconds);
+        executor.setMaxPoolSize(pool.getMaxSize());
+        executor.setCorePoolSize(pool.getCoreSize());
+        executor.setQueueCapacity(pool.getQueueCapacity());
+        executor.setKeepAliveSeconds((int)pool.getKeepAlive().getSeconds());
         // 线程池对拒绝任务(无线程可用)的处理策略
-        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        //executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
         return executor;
     }