Explorar o código

Merge branch 'release/1.6.7'

tianyunperfect %!s(int64=4) %!d(string=hai) anos
pai
achega
39f760a469

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 # 版本升级日志
 
+## 1.6.7 - 2021年2月3日
+
+- 增加线程池工具类
+
 ## 1.6.6 - 2020年12月19日
 
 - 封装controller返回值

+ 1 - 0
README.md

@@ -13,6 +13,7 @@ springboot-web常用功能整合,包含常用功能。
 - 分布式Id生成器(雪花算法,利用hostName和Ip自动分配,系统多于32个的时候,请使用redis或者mysql来分配。)
 - 日期工具类
 - 爬虫工具类
+- 线程池工具类
 
 ## 怎么使用
 

+ 8 - 0
pom.xml

@@ -25,6 +25,14 @@
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>versions-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
 
     <!--锁定springCloud版本-->
     <dependencyManagement>

+ 6 - 0
springboot-common/pom.xml

@@ -31,6 +31,12 @@
             <version>3.8</version>
         </dependency>
 
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+        </dependency>
+
         <!--httpClient-->
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>

+ 1 - 1
springboot-common/src/main/java/com/alvin/common/util/DateUtil.java → springboot-common/src/main/java/com/alvin/common/util/DateUtils.java

@@ -15,7 +15,7 @@ import java.time.temporal.TemporalAdjusters;
  * @author tianyunperfect
  * @date 2020/05/28
  */
-public class DateUtil {
+public class DateUtils {
 
     /**
      * 当前秒

+ 1 - 1
springboot-common/src/main/java/com/alvin/common/util/EmailUtil.java → springboot-common/src/main/java/com/alvin/common/util/EmailUtils.java

@@ -1,4 +1,4 @@
 package com.alvin.common.util;
 
-public class EmailUtil {
+public class EmailUtils {
 }

+ 38 - 0
springboot-common/src/main/java/com/alvin/common/util/FileUtils.java

@@ -0,0 +1,38 @@
+package com.alvin.common.util;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 常用文件工具类
+ * 1、读取所有行请使用common-io工具类
+ * 2、读取单行数据可以使用
+ *
+ * @author tianyunperfect
+ * @date 2021/01/15
+ */
+public class FileUtils {
+    /**
+     * 获取到 BF,一定要手动关闭
+     *
+     * @param filePath 文件路径
+     * @param encode   编码
+     * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
+     */
+    public static BufferedReader getBufferedReader(String filePath, String encode) throws FileNotFoundException, UnsupportedEncodingException {
+        return new BufferedReader(
+                new InputStreamReader(
+                        new FileInputStream(filePath), encode));
+    }
+
+    /**
+     * 获取到 BF,一定要手动关闭,默认UTF-8
+     *
+     * @param filePath 文件路径
+     * @return {@link BufferedReader}* @throws FileNotFoundException 文件未发现异常
+     * @throws UnsupportedEncodingException 不支持的编码异常
+     */
+    public static BufferedReader getBufferedReader(String filePath) throws FileNotFoundException, UnsupportedEncodingException {
+        return getBufferedReader(filePath, StandardCharsets.UTF_8.name());
+    }
+}

+ 5 - 5
springboot-common/src/main/java/com/alvin/common/util/HttpUtil.java → springboot-common/src/main/java/com/alvin/common/util/HttpUtils.java

@@ -40,7 +40,7 @@ import java.util.Map;
 
 @Data
 @Accessors(chain = true)
-public class HttpUtil {
+public class HttpUtils {
 
 
     /** 代理  */
@@ -65,7 +65,7 @@ public class HttpUtil {
      */
     private List<NameValuePair> nameValuePairs = new ArrayList<>();
 
-    public HttpUtil(String urlStr) {
+    public HttpUtils(String urlStr) {
         // 5秒
         this.urlStr = urlStr;
     }
@@ -76,7 +76,7 @@ public class HttpUtil {
      * @param paramsMap
      * @throws URISyntaxException
      */
-    public HttpUtil setParams(HashMap<String, String> paramsMap){
+    public HttpUtils setParams(HashMap<String, String> paramsMap){
         for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
             nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
         }
@@ -261,7 +261,7 @@ public class HttpUtil {
      * @param port
      * @param httpType
      */
-    public HttpUtil setProxy(String ip, int port, String httpType) {
+    public HttpUtils setProxy(String ip, int port, String httpType) {
         this.proxy = new HttpHost(ip, port, httpType);
         return this;
     }
@@ -363,7 +363,7 @@ public class HttpUtil {
     }
 
     public static void main(String[] args) throws Exception {
-        String s = new HttpUtil("https://www.baidu.com").get();
+        String s = new HttpUtils("https://www.baidu.com").get();
         System.out.println(s);
     }
 }

+ 2 - 2
springboot-common/src/main/java/com/alvin/common/util/JsonUtil.java → springboot-common/src/main/java/com/alvin/common/util/JsonUtils.java

@@ -17,10 +17,10 @@ import java.util.Map;
  * @author tianyunperfect
  * @date 2020/05/20
  */
-public class JsonUtil {
+public class JsonUtils {
     private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
 
-    public JsonUtil() {
+    public JsonUtils() {
     }
 
     /**

+ 80 - 0
springboot-common/src/main/java/com/alvin/common/util/ThreadPoolUtils.java

@@ -0,0 +1,80 @@
+package com.alvin.common.util;
+
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * 线程池工具类
+ *
+ * @author tianyunperfect
+ * @date 2021/02/03
+ */
+public class ThreadPoolUtils {
+
+    /**
+     * 获取线程池
+     *
+     * @param corePoolSize    核心池大小
+     * @param maximumPoolSize 最大池大小
+     * @param keepAliveTime   维持时间
+     * @param unit            单位
+     * @param queueSize       队列的大小
+     * @param poolName        池名称
+     * @param handler         拒绝策略
+     * @return {@link ThreadPoolExecutor}
+     */
+    public static ThreadPoolExecutor getThreadPool(
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            int queueSize,
+            String poolName,
+            RejectedExecutionHandler handler) {
+        return new ThreadPoolExecutor(
+                corePoolSize, maximumPoolSize,
+                keepAliveTime, unit,
+                new LinkedBlockingDeque<>(queueSize),
+                new MyThreadFactory(poolName),
+                handler
+        );
+    }
+
+
+    /**
+     * 修改于默认线程池工程类,添加了自定义线程名
+     *
+     * @author tianyunperfect
+     * @date 2021/02/03
+     */
+    public static class MyThreadFactory implements ThreadFactory {
+        private static final AtomicInteger poolNumber = new AtomicInteger(1);
+        private final ThreadGroup group;
+        private final AtomicInteger threadNumber = new AtomicInteger(1);
+        private final String namePrefix;
+
+        MyThreadFactory(String name) {
+            SecurityManager s = System.getSecurityManager();
+            group = (s != null) ? s.getThreadGroup() :
+                    Thread.currentThread().getThreadGroup();
+            //自定义名称
+            if (name == null || name.isEmpty()) {
+                name = "pool";
+            }
+            namePrefix = name + "-" +
+                    poolNumber.getAndIncrement() +
+                    "-thread-";
+        }
+
+        public Thread newThread(Runnable r) {
+            Thread t = new Thread(group, r,
+                    namePrefix + threadNumber.getAndIncrement(),
+                    0);
+            if (t.isDaemon())
+                t.setDaemon(false);
+            if (t.getPriority() != Thread.NORM_PRIORITY)
+                t.setPriority(Thread.NORM_PRIORITY);
+            return t;
+        }
+    }
+}

+ 67 - 0
springboot-main/src/main/java/com/alvin/arthas/AopTargetUtils.java

@@ -0,0 +1,67 @@
+package com.alvin.arthas;
+
+import org.springframework.aop.framework.AdvisedSupport;
+import org.springframework.aop.framework.AopProxy;
+import org.springframework.aop.support.AopUtils;
+
+import java.lang.reflect.Field;
+
+/**
+ * 用于arthas获取AOP的代理对象。
+ * 示例:
+ * ognl -x 3 '#springContext=@com.alvin.arthas.ApplicationContextProvider@context,#proxyBean=#springContext.getBean("lightEventPublisher"),@com.alvin.arthas.AopTargetUtils@getTarget(#proxyBean).test' -c 20ad9418
+ *
+ * @author tianyunperfect
+ * @date 2020/12/31
+ */
+public class AopTargetUtils {
+
+
+    /**
+     * 获取 目标对象
+     * @param proxy 代理对象
+     * @return
+     * @throws Exception
+     */
+    public static Object getTarget(Object proxy) throws Exception {
+
+        if(!AopUtils.isAopProxy(proxy)) {
+            return proxy; //不是代理对象
+        }
+
+        if(AopUtils.isJdkDynamicProxy(proxy)) {
+            return getJdkDynamicProxyTargetObject(proxy);
+        } else { //cglib
+            return getCglibProxyTargetObject(proxy);
+        }
+
+    }
+
+
+    private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
+        Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
+        h.setAccessible(true);
+        Object dynamicAdvisedInterceptor = h.get(proxy);
+
+        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
+        advised.setAccessible(true);
+
+        Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
+
+        return target;
+    }
+
+    private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
+        Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
+        h.setAccessible(true);
+        AopProxy aopProxy = (AopProxy) h.get(proxy);
+
+        Field advised = aopProxy.getClass().getDeclaredField("advised");
+        advised.setAccessible(true);
+
+        Object target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget();
+
+        return target;
+    }
+
+}

+ 20 - 0
springboot-main/src/main/java/com/alvin/arthas/ApplicationContextProvider.java

@@ -0,0 +1,20 @@
+package com.alvin.arthas;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ApplicationContextProvider implements ApplicationContextAware {
+
+    private static ApplicationContext context;
+
+    public ApplicationContext getApplicationContext() {
+        return context;
+    }
+
+    @Override
+    public void setApplicationContext(ApplicationContext ctx) {
+        context = ctx;
+    }
+}