This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 9dd1a1b46 [ISSUE #3026] [type:new feature] part4 MotanPlugin support 
using shared pool (#3253)
9dd1a1b46 is described below

commit 9dd1a1b461150918b57496c1f77a4e55ba305751
Author: dragon-zhang <[email protected]>
AuthorDate: Mon Apr 18 13:53:54 2022 +0800

    [ISSUE #3026] [type:new feature] part4 MotanPlugin support using shared 
pool (#3253)
    
    * [ISSUE #3026] [type:new feature] MotanPlugin support using shared pool
    
    * fix sql
    
    * use Constants
    
    * fix sql
    
    * fix npe
    
    * enable not execute sql
---
 script/2.4.3-upgrade-2.5.0-mysql.sql               |  8 +++-
 script/2.4.3-upgrade-2.5.0-pg.sql                  |  8 +++-
 .../src/main/resources/sql-script/h2/schema.sql    |  3 +-
 .../src/main/resources/sql-script/mysql/schema.sql |  3 +-
 .../src/main/resources/sql-script/pg/schema.sql    |  3 +-
 .../apache/shenyu/common/constant/Constants.java   | 27 +++++++++++-
 .../dto/convert/plugin/MotanRegisterConfig.java    | 30 +++++++++++--
 .../plugin/grpc/client/GrpcClientBuilder.java      | 24 ++++-------
 .../plugin/motan/proxy/MotanProxyService.java      | 50 ++++++++++++++++------
 9 files changed, 118 insertions(+), 38 deletions(-)

diff --git a/script/2.4.3-upgrade-2.5.0-mysql.sql 
b/script/2.4.3-upgrade-2.5.0-mysql.sql
index 4323aaccc..5f18020a6 100644
--- a/script/2.4.3-upgrade-2.5.0-mysql.sql
+++ b/script/2.4.3-upgrade-2.5.0-mysql.sql
@@ -24,4 +24,10 @@
 -- insert plugin_handle data for motan
 INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270286164094976', '17', 
'corethreads', 'corethreads', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"corethreads","rule":""}');
 INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270476329644032', '17', 
'threads', 'threads', 1, 3, 0, 
'{"required":"0","defaultValue":"2147483647","placeholder":"threads","rule":""}');
-INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270555383885824', '17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
\ No newline at end of file
+INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270555383885824', '17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
+INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1515116191850078208', '17', 
'threadpool', 'threadpool', 3, 3, 0, 
'{"required":"0","defaultValue":"cached","placeholder":"threadpool","rule":""}');
+
+-- Note: it doesn't matter if you don't execute this SQL
+-- Note: because most users have changed ZK configuration, this SQL is 
annotated to prevent erroneous execution
+-- UPDATE plugin SET 
config='{"register":"zookeeper://localhost:2181","corethreads":0,"threads":2147483647,"queues":0,"threadpool":"cached"}'
 WHERE `name` = 'motan';
+--                                                    ^^^^^^^^^^^^^^ if you 
want to execute this SQL, please replace it with your own ZK configuration
diff --git a/script/2.4.3-upgrade-2.5.0-pg.sql 
b/script/2.4.3-upgrade-2.5.0-pg.sql
index ab03e6f87..d67e81dec 100644
--- a/script/2.4.3-upgrade-2.5.0-pg.sql
+++ b/script/2.4.3-upgrade-2.5.0-pg.sql
@@ -24,4 +24,10 @@
 -- insert plugin_handle data for motan
 INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270286164094976', '17', 
'corethreads', 'corethreads', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"corethreads","rule":""}');
 INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270476329644032', '17', 
'threads', 'threads', 1, 3, 0, 
'{"required":"0","defaultValue":"2147483647","placeholder":"threads","rule":""}');
-INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270555383885824', '17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
\ No newline at end of file
+INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1510270555383885824', '17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
+INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`, `field`, `label`, 
`data_type`, `type`, `sort`, `ext_obj`) VALUES ('1515116191850078208', '17', 
'threadpool', 'threadpool', 3, 3, 0, 
'{"required":"0","defaultValue":"cached","placeholder":"threadpool","rule":""}');
+
+-- Note: it doesn't matter if you don't execute this SQL
+-- Note: because most users have changed ZK configuration, this SQL is 
annotated to prevent erroneous execution
+-- UPDATE plugin SET 
config='{"register":"zookeeper://localhost:2181","corethreads":0,"threads":2147483647,"queues":0,"threadpool":"cached"}'
 WHERE `name` = 'motan';
+--                                                    ^^^^^^^^^^^^^^ if you 
want to execute this SQL, please replace it with your own ZK configuration
diff --git a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql 
b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
index 7ced265c4..611096676 100644
--- a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
@@ -349,7 +349,7 @@ INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, 
`config`, `enabled`)
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('14', 'contextPath', 'HttpProcess', 80,'1');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('15', 'grpc', 'Proxy', 
310,'{"multiSelectorHandle":"1","multiRuleHandle":"0","threadpool":"cached"}','0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('16', 'redirect', 'HttpProcess', 110,'0');
-INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('17', 'motan', 'Proxy', 
310,'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0}','0');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('17', 'motan', 'Proxy', 
310,'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0,"threadpool":"cached"}','0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('18', 'logging', 'Logging', 160, '0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('19', 'jwt', 'Authentication', 30, '{"secretKey":"key"}', 
'0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('20', 'request', 'HttpProcess', 120, '0');
@@ -526,6 +526,7 @@ INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'corethreads', 'corethreads', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"corethreads","rule":""}');
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'threads', 'threads', 1, 3, 0, 
'{"required":"0","defaultValue":"2147483647","placeholder":"threads","rule":""}');
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
+INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'threadpool', 'threadpool', 3, 3, 0, 
'{"required":"0","defaultValue":"cached","placeholder":"threadpool","rule":""}');
 
 /*insert plugin_handle data for plugin mqtt*/
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('28', 
'port', 'port', 1, 3, 1, NULL);
diff --git a/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql 
b/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
index 1b130fa9a..2af2cc761 100644
--- a/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
@@ -354,7 +354,7 @@ INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, 
`config`, `enabled`)
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('14', 'contextPath', 'HttpProcess', 80,'1');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('15', 'grpc', 'Proxy', 
310,'{"multiSelectorHandle":"1","multiRuleHandle":"0","threadpool":"cached"}','0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('16', 'redirect', 'HttpProcess', 110,'0');
-INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('17', 'motan', 'Proxy', 
310,'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0}','0');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('17', 'motan', 'Proxy', 
310,'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0,"threadpool":"cached"}','0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('18', 'logging', 'Logging', 160, '0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, 
`enabled`) VALUES ('19', 'jwt', 'Authentication', 30, '{"secretKey":"key"}', 
'0');
 INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`) VALUES 
('20', 'request', 'HttpProcess', 120, '0');
@@ -531,6 +531,7 @@ INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'corethreads', 'corethreads', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"corethreads","rule":""}');
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'threads', 'threads', 1, 3, 0, 
'{"required":"0","defaultValue":"2147483647","placeholder":"threads","rule":""}');
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'queues', 'queues', 1, 3, 0, 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}');
+INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('17', 
'threadpool', 'threadpool', 3, 3, 0, 
'{"required":"0","defaultValue":"cached","placeholder":"threadpool","rule":""}');
 
 /*insert plugin_handle data for plugin mqtt*/
 INSERT IGNORE INTO plugin_handle 
(`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('28', 
'port', 'port', 1, 3, 1, NULL);
diff --git a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql 
b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
index 06a7354b8..82be47c35 100644
--- a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
@@ -366,7 +366,7 @@ ELSE
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '14' || ''', ''' || 'contextPath' || ''', NULL, ''' || 'HttpProcess' || 
''', 80, 1);');
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '15' || ''', ''' || 'grpc' || ''', ''' || 
'{"multiSelectorHandle":"1","multiRuleHandle":"0","threadpool":"cached"}' || 
''', ''' || 'Proxy' || ''', 310, 0);');
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '16' || ''', ''' || 'redirect' || ''', NULL, ''' || 'HttpProcess' || 
''', 110, 0);');
-       PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '17' || ''', ''' || 'motan' || ''', ''' || 
'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0}' 
|| ''', ''' || 'Proxy' || ''', 310, 0);');
+       PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '17' || ''', ''' || 'motan' || ''', ''' || 
'{"register":"127.0.0.1:2181","corethreads":0,"threads":2147483647,"queues":0,"threadpool":"cached"}'
 || ''', ''' || 'Proxy' || ''', 310, 0);');
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '18' || ''', ''' || 'logging' || ''', NULL, ''' || 'Logging' || ''', 
160, 0);');
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '19' || ''', ''' || 'jwt' || ''', ''' || '{"secretKey":"key"}' || ''', 
''' || 'Authentication' || ''', 30, 0);');
        PERFORM public.dblink_exec('init_conn',  'INSERT INTO "plugin" VALUES 
(''' || '2' || ''', ''' || 'waf' || ''', ''' || '{"model":"black"}' || ''', ''' 
|| 'Authentication' || ''', 50, 0);');
@@ -612,6 +612,7 @@ ELSE
     PERFORM public.dblink_exec('init_conn',  'INSERT  INTO "plugin_handle" ( 
plugin_id , field , label , data_type , type , sort , ext_obj ) VALUES (''' || 
'17' || ''', ''' || 'corethreads' || ''', ''' || 'corethreads' || ''', 1, 3, 0, 
''' || 
'{"required":"0","defaultValue":"0","placeholder":"corethreads","rule":""}' || 
''');');
     PERFORM public.dblink_exec('init_conn',  'INSERT  INTO "plugin_handle" ( 
plugin_id , field , label , data_type , type , sort , ext_obj ) VALUES (''' || 
'17' || ''', ''' || 'threads' || ''', ''' || 'threads' || ''', 1, 3, 0, ''' || 
'{"required":"0","defaultValue":"2147483647","placeholder":"threads","rule":""}'
 || ''');');
     PERFORM public.dblink_exec('init_conn',  'INSERT  INTO "plugin_handle" ( 
plugin_id , field , label , data_type , type , sort , ext_obj ) VALUES (''' || 
'17' || ''', ''' || 'queues' || ''', ''' || 'queues' || ''', 1, 3, 0, ''' || 
'{"required":"0","defaultValue":"0","placeholder":"queues","rule":""}' || 
''');');
+    PERFORM public.dblink_exec('init_conn',  'INSERT  INTO "plugin_handle" ( 
plugin_id , field , label , data_type , type , sort , ext_obj ) VALUES (''' || 
'17' || ''', ''' || 'threadpool' || ''', ''' || 'threadpool' || ''', 3, 3, 0, 
''' || 
'{"required":"0","defaultValue":"cached","placeholder":"threadpool","rule":""}' 
|| ''');');
 
     /*insert plugin_handle data for plugin mqtt*/
     PERFORM public.dblink_exec('init_conn',  'INSERT  INTO "plugin_handle" ( 
plugin_id , field , label , data_type , type , sort , ext_obj ) VALUES (''' || 
'28' || ''', ''' || 'port' || ''', ''' || 'port' || ''', 1, 3, 1, null);');
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
index dc06c2072..69a964c01 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
@@ -346,7 +346,7 @@ public interface Constants {
      * jwt handle key for secretKey.
      */
     String SECRET_KEY = "secretKey";
-    
+
     /**
      * local key.
      */
@@ -597,6 +597,31 @@ public interface Constants {
      */
     String ZOMBIE_REMOVAL_TIMES_VALUE = "60";
 
+    /**
+     * shared thread pool type.
+     */
+    String SHARED = "shared";
+
+    /**
+     * fixed thread pool type.
+     */
+    String FIXED = "fixed";
+
+    /**
+     * eager thread pool type.
+     */
+    String EAGER = "eager";
+
+    /**
+     * limited thread pool type.
+     */
+    String LIMITED = "limited";
+
+    /**
+     * cached thread pool type.
+     */
+    String CACHED = "cached";
+
     /**
      * String q.
      */
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/plugin/MotanRegisterConfig.java
 
b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/plugin/MotanRegisterConfig.java
index b4d44e0bc..f4f29b9be 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/plugin/MotanRegisterConfig.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/plugin/MotanRegisterConfig.java
@@ -29,12 +29,32 @@ public class MotanRegisterConfig implements Serializable {
 
     private String register;
 
+    private String threadpool;
+
     private Integer corethreads;
 
     private Integer threads;
 
     private Integer queues;
 
+    /**
+     * get threadpool.
+     *
+     * @return threadpool
+     */
+    public String getThreadpool() {
+        return threadpool;
+    }
+
+    /**
+     * set threadpool.
+     *
+     * @param threadpool threadpool
+     */
+    public void setThreadpool(final String threadpool) {
+        this.threadpool = threadpool;
+    }
+
     /**
      * get register.
      *
@@ -116,13 +136,14 @@ public class MotanRegisterConfig implements Serializable {
             return false;
         }
         MotanRegisterConfig that = (MotanRegisterConfig) o;
-        return Objects.equals(register, that.register) && 
Objects.equals(corethreads, that.corethreads)
-                && Objects.equals(threads, that.threads) && 
Objects.equals(queues, that.queues);
+        return Objects.equals(register, that.register) && 
Objects.equals(threadpool, that.threadpool)
+                && Objects.equals(corethreads, that.corethreads) && 
Objects.equals(threads, that.threads)
+                && Objects.equals(queues, that.queues);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(register, corethreads, threads, queues);
+        return Objects.hash(register, threadpool, corethreads, threads, 
queues);
     }
 
     @Override
@@ -131,6 +152,9 @@ public class MotanRegisterConfig implements Serializable {
                 + "register='"
                 + register
                 + '\''
+                + ", threadpool='"
+                + threadpool
+                + '\''
                 + ", corethreads='"
                 + corethreads
                 + '\''
diff --git 
a/shenyu-plugin/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
 
b/shenyu-plugin/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
index e54dee30b..bbbf32c09 100644
--- 
a/shenyu-plugin/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
+++ 
b/shenyu-plugin/shenyu-plugin-grpc/src/main/java/org/apache/shenyu/plugin/grpc/client/GrpcClientBuilder.java
@@ -22,6 +22,7 @@ import io.grpc.ManagedChannel;
 import io.grpc.ManagedChannelBuilder;
 import io.grpc.NameResolverRegistry;
 import org.apache.shenyu.common.concurrent.ShenyuThreadPoolExecutor;
+import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.dto.convert.plugin.GrpcRegisterConfig;
 import org.apache.shenyu.common.exception.ShenyuException;
 import org.apache.shenyu.common.utils.Singleton;
@@ -33,6 +34,7 @@ import 
org.apache.shenyu.plugin.grpc.loadbalance.RoundRobinLoadBalancerProvider;
 import org.apache.shenyu.plugin.grpc.resolver.ShenyuNameResolverProvider;
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 
+import java.util.Optional;
 import java.util.concurrent.Executor;
 
 /**
@@ -40,16 +42,6 @@ import java.util.concurrent.Executor;
  */
 public final class GrpcClientBuilder {
 
-    private static final String SHARED = "shared";
-
-    private static final String FIXED = "fixed";
-
-    private static final String EAGER = "eager";
-
-    private static final String LIMITED = "limited";
-
-    private static final String CACHED = "cached";
-
     static {
         LoadBalancerRegistry.getDefaultRegistry().register(new 
RandomLoadBalancerProvider());
         LoadBalancerRegistry.getDefaultRegistry().register(new 
RoundRobinLoadBalancerProvider());
@@ -83,19 +75,19 @@ public final class GrpcClientBuilder {
         if (null == config) {
             return null;
         }
-        final String threadpool = config.getThreadpool();
+        final String threadpool = 
Optional.ofNullable(config.getThreadpool()).orElse(Constants.CACHED);
         switch (threadpool) {
-            case SHARED:
+            case Constants.SHARED:
                 try {
                     return 
SpringBeanUtils.getInstance().getBean(ShenyuThreadPoolExecutor.class);
                 } catch (NoSuchBeanDefinitionException t) {
                     throw new ShenyuException("shared thread pool is not 
enable, config ${shenyu.sharedPool.enable} in your xml/yml !", t);
                 }
-            case FIXED:
-            case EAGER:
-            case LIMITED:
+            case Constants.FIXED:
+            case Constants.EAGER:
+            case Constants.LIMITED:
                 throw new UnsupportedOperationException();
-            case CACHED:
+            case Constants.CACHED:
             default:
                 return null;
         }
diff --git 
a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
 
b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
index 1f2e99c58..1eb0486af 100644
--- 
a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
+++ 
b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
@@ -23,6 +23,7 @@ import com.weibo.api.motan.rpc.ResponseFuture;
 import com.weibo.api.motan.rpc.RpcContext;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
+import org.apache.shenyu.common.concurrent.ShenyuThreadPoolExecutor;
 import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.common.dto.MetaData;
 import org.apache.shenyu.common.dto.convert.plugin.MotanRegisterConfig;
@@ -31,9 +32,11 @@ import org.apache.shenyu.common.enums.ResultEnum;
 import org.apache.shenyu.common.exception.ShenyuException;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.common.utils.Singleton;
+import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
 import org.apache.shenyu.plugin.motan.cache.ApplicationConfigCache;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 import org.springframework.web.server.ServerWebExchange;
 import reactor.core.publisher.Mono;
 
@@ -102,19 +105,7 @@ public class MotanProxyService {
             return null;
         }
         //CHECKSTYLE:ON IllegalCatch
-        if (Objects.isNull(threadPool)) {
-            MotanRegisterConfig config = 
Singleton.INST.get(MotanRegisterConfig.class);
-            if (Objects.isNull(config)) {
-                // should not execute to here
-                threadPool = Executors.newCachedThreadPool(factory);
-            } else {
-                int corePoolSize = 
Optional.ofNullable(config.getCorethreads()).orElse(0);
-                int maximumPoolSize = 
Optional.ofNullable(config.getThreads()).orElse(Integer.MAX_VALUE);
-                int queueSize = 
Optional.ofNullable(config.getThreads()).orElse(0);
-                threadPool = new ThreadPoolExecutor(corePoolSize, 
maximumPoolSize, 60L, TimeUnit.SECONDS,
-                        queueSize > 0 ? new LinkedBlockingQueue<>(queueSize) : 
new SynchronousQueue<>(), factory);
-            }
-        }
+        initThreadPool();
         CompletableFuture<Object> future = 
CompletableFuture.supplyAsync(responseFuture::getValue, threadPool);
         return Mono.fromFuture(future.thenApply(ret -> {
             if (Objects.isNull(ret)) {
@@ -125,4 +116,37 @@ public class MotanProxyService {
             return ret;
         })).onErrorMap(ShenyuException::new);
     }
+
+    private void initThreadPool() {
+        if (Objects.nonNull(threadPool)) {
+            return;
+        }
+        MotanRegisterConfig config = 
Singleton.INST.get(MotanRegisterConfig.class);
+        if (Objects.isNull(config)) {
+            // should not execute to here
+            threadPool = Executors.newCachedThreadPool(factory);
+            return;
+        }
+        final String threadpool = 
Optional.ofNullable(config.getThreadpool()).orElse(Constants.CACHED);
+        switch (threadpool) {
+            case Constants.SHARED:
+                try {
+                    threadPool = 
SpringBeanUtils.getInstance().getBean(ShenyuThreadPoolExecutor.class);
+                    return;
+                } catch (NoSuchBeanDefinitionException t) {
+                    throw new ShenyuException("shared thread pool is not 
enable, config ${shenyu.sharedPool.enable} in your xml/yml !", t);
+                }
+            case Constants.FIXED:
+            case Constants.EAGER:
+            case Constants.LIMITED:
+                throw new UnsupportedOperationException();
+            case Constants.CACHED:
+            default:
+                int corePoolSize = 
Optional.ofNullable(config.getCorethreads()).orElse(0);
+                int maximumPoolSize = 
Optional.ofNullable(config.getThreads()).orElse(Integer.MAX_VALUE);
+                int queueSize = 
Optional.ofNullable(config.getThreads()).orElse(0);
+                threadPool = new ThreadPoolExecutor(corePoolSize, 
maximumPoolSize, 60L, TimeUnit.SECONDS,
+                        queueSize > 0 ? new LinkedBlockingQueue<>(queueSize) : 
new SynchronousQueue<>(), factory);
+        }
+    }
 }

Reply via email to