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

zhangzicheng 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 d47e617a2 [ISSUE #3173] support configurable timeout for MotanPlugin 
(#3363)
d47e617a2 is described below

commit d47e617a2eeedb6d9e0aaeb2faf08cc31ad31fa6
Author: ChineseTony <[email protected]>
AuthorDate: Sat Apr 30 19:30:21 2022 +0800

    [ISSUE #3173] support configurable timeout for MotanPlugin (#3363)
    
    * support configurable timeout for MotanPlugin
---
 .../motan/MotanServiceBeanPostProcessor.java       | 10 +++++++---
 .../client/motan/common/dto/MotanRpcExt.java       | 23 +++++++++++++++++++++-
 .../examples/motan/service/MotanDemoService.java   |  1 +
 .../motan/service/TestMotanApplication.java        |  1 +
 .../motan/service/config/RegistryProperties.java   |  2 ++
 .../motan/service/impl/MotanDemoServiceImpl.java   | 11 +++++++++++
 .../plugin/motan/cache/ApplicationConfigCache.java | 13 +++++++++++-
 7 files changed, 56 insertions(+), 5 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
 
b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
index 1af28c30a..3c9094614 100644
--- 
a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
+++ 
b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
@@ -46,6 +46,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -76,6 +77,8 @@ public class MotanServiceBeanPostProcessor implements 
BeanPostProcessor, Applica
 
     private String group;
 
+    private Integer timeout;
+
     public MotanServiceBeanPostProcessor(final PropertiesConfig clientConfig, 
final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
         Properties props = clientConfig.getProps();
         String contextPath = 
props.getProperty(ShenyuClientConstants.CONTEXT_PATH);
@@ -108,6 +111,7 @@ public class MotanServiceBeanPostProcessor implements 
BeanPostProcessor, Applica
         if (group == null) {
             group = ((BasicServiceConfigBean) 
applicationContext.getBean(BASE_SERVICE_CONFIG)).getGroup();
         }
+        timeout = Optional.ofNullable(((BasicServiceConfigBean) 
applicationContext.getBean(BASE_SERVICE_CONFIG)).getRequestTimeout()).orElse(1000);
         Class<?> clazz = bean.getClass();
         if (AopUtils.isAopProxy(bean)) {
             clazz = AopUtils.getTargetClass(bean);
@@ -118,7 +122,7 @@ public class MotanServiceBeanPostProcessor implements 
BeanPostProcessor, Applica
             ShenyuMotanClient shenyuMotanClient = 
method.getAnnotation(ShenyuMotanClient.class);
             if (Objects.nonNull(shenyuMotanClient)) {
                 publisher.publishEvent(buildMetaDataDTO(clazz, service,
-                        shenyuMotanClient, method, buildRpcExt(methods)));
+                        shenyuMotanClient, method, buildRpcExt(methods, 
timeout)));
             }
         }
     }
@@ -176,7 +180,7 @@ public class MotanServiceBeanPostProcessor implements 
BeanPostProcessor, Applica
         return new MotanRpcExt.RpcExt(method.getName(), params);
     }
 
-    private String buildRpcExt(final Method[] methods) {
+    private String buildRpcExt(final Method[] methods, final Integer timeout) {
         List<MotanRpcExt.RpcExt> list = new ArrayList<>();
         for (Method method : methods) {
             ShenyuMotanClient shenyuMotanClient = 
method.getAnnotation(ShenyuMotanClient.class);
@@ -184,7 +188,7 @@ public class MotanServiceBeanPostProcessor implements 
BeanPostProcessor, Applica
                 list.add(buildRpcExt(method));
             }
         }
-        MotanRpcExt buildList = new MotanRpcExt(list, group);
+        MotanRpcExt buildList = new MotanRpcExt(list, group, timeout);
         return GsonUtils.getInstance().toJson(buildList);
     }
 
diff --git 
a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/common/dto/MotanRpcExt.java
 
b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/common/dto/MotanRpcExt.java
index c65deb0d5..9834c5399 100644
--- 
a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/common/dto/MotanRpcExt.java
+++ 
b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/common/dto/MotanRpcExt.java
@@ -31,6 +31,8 @@ public class MotanRpcExt {
 
     private String group;
 
+    private Integer timeout;
+
     /**
      * constructor without params.
      */
@@ -43,9 +45,10 @@ public class MotanRpcExt {
      * @param methodInfo methodInfo
      * @param group group
      */
-    public MotanRpcExt(final List<RpcExt> methodInfo, final String group) {
+    public MotanRpcExt(final List<RpcExt> methodInfo, final String group, 
final Integer timeout) {
         this.methodInfo = methodInfo;
         this.group = group;
+        this.timeout = timeout;
     }
 
     /**
@@ -83,11 +86,29 @@ public class MotanRpcExt {
         this.group = group;
     }
 
+    /**
+     * get timeout.
+     *
+     * @return timeout
+     */
+    public Integer getTimeout() {
+        return timeout;
+    }
+
+    /**
+     * set timeout.
+     * @param timeout timeout
+     */
+    public void setTimeout(final Integer timeout) {
+        this.timeout = timeout;
+    }
+
     @Override
     public String toString() {
         return "MotanRpcExt{"
                 + "methodInfo=" + methodInfo
                 + ", group='" + group + '\''
+                + ", timeout='" + timeout + '\''
                 + '}';
     }
 
diff --git 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-api/src/main/java/org/apache/shenyu/examples/motan/service/MotanDemoService.java
 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-api/src/main/java/org/apache/shenyu/examples/motan/service/MotanDemoService.java
index 023b8910c..56aaa3309 100644
--- 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-api/src/main/java/org/apache/shenyu/examples/motan/service/MotanDemoService.java
+++ 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-api/src/main/java/org/apache/shenyu/examples/motan/service/MotanDemoService.java
@@ -25,4 +25,5 @@ import com.weibo.api.motan.transport.async.MotanAsync;
 @MotanAsync
 public interface MotanDemoService {
     String hello(String name);
+    String testTimeOut(String timeout);
 }
diff --git 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/TestMotanApplication.java
 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/TestMotanApplication.java
index 3f8dd3923..7715f4578 100644
--- 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/TestMotanApplication.java
+++ 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/TestMotanApplication.java
@@ -78,6 +78,7 @@ public class TestMotanApplication {
         config.setModule("motan-demo-rpc");
         config.setApplication("myMotanDemo");
         config.setRegistry("registryConfig1");
+        config.setRequestTimeout(2000);
         return config;
     }
 }
diff --git 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/config/RegistryProperties.java
 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/config/RegistryProperties.java
index aab83b5ad..7ac7091a2 100644
--- 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/config/RegistryProperties.java
+++ 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/config/RegistryProperties.java
@@ -31,6 +31,7 @@ public class RegistryProperties {
 
     private String address;
 
+
     /**
      * Get the protocol.
      *
@@ -66,4 +67,5 @@ public class RegistryProperties {
     public void setAddress(final String address) {
         this.address = address;
     }
+
 }
diff --git 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
index dd67b42fc..52d3f5059 100644
--- 
a/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
+++ 
b/shenyu-examples/shenyu-examples-motan/shenyu-examples-motan-service/src/main/java/org/apache/shenyu/examples/motan/service/impl/MotanDemoServiceImpl.java
@@ -32,4 +32,15 @@ public class MotanDemoServiceImpl implements 
MotanDemoService {
     public String hello(final String name) {
         return "hello " + name;
     }
+
+    @Override
+    @ShenyuMotanClient(path = "/timeout")
+    public String testTimeOut(final String timeout)  {
+        try {
+            Thread.sleep((long) (Double.parseDouble(timeout) * 1000));
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+        return "hello timeout " + timeout + "s";
+    }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/cache/ApplicationConfigCache.java
 
b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/cache/ApplicationConfigCache.java
index b6033b517..e8c847059 100644
--- 
a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/cache/ApplicationConfigCache.java
+++ 
b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/cache/ApplicationConfigCache.java
@@ -41,6 +41,7 @@ import org.springframework.lang.NonNull;
 import java.lang.reflect.Field;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutionException;
@@ -185,7 +186,7 @@ public final class ApplicationConfigCache {
         });
         reference.setGroup(motanParamExtInfo.getGroup());
         reference.setVersion("1.0");
-        reference.setRequestTimeout(1000);
+        
reference.setRequestTimeout(Optional.ofNullable(motanParamExtInfo.getTimeout()).orElse(1000));
         reference.setRegistry(registryConfig);
         reference.setProtocol(protocolConfig);
         CommonHandler obj = reference.getRef();
@@ -281,6 +282,8 @@ public final class ApplicationConfigCache {
         private List<MethodInfo> methodInfo;
         
         private String group;
+
+        private Integer timeout;
         
         /**
          * Gets method info.
@@ -317,6 +320,14 @@ public final class ApplicationConfigCache {
         public void setGroup(final String group) {
             this.group = group;
         }
+
+        public Integer getTimeout() {
+            return timeout;
+        }
+
+        public void setTimeout(final Integer timeout) {
+            this.timeout = timeout;
+        }
     }
     
     /**

Reply via email to