You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2017/12/27 09:14:42 UTC

[incubator-servicecomb-java-chassis] 01/02: JAV-592 producer support return CompletableFuture, and support different method name between swagger and producer

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit acb84cf427d71bfa8c1f1abdef1caf1f60035cab
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Dec 26 14:19:56 2017 +0800

    JAV-592 producer support return CompletableFuture, and support different method name between swagger and producer
---
 .../swagger/engine/SwaggerEnvironment.java         | 16 +++++++---
 .../swagger/engine/SwaggerProducerOperation.java   | 36 ++++++++++++++++++++--
 .../engine/unittest/LocalProducerInvoker.java      | 13 +++++---
 .../swagger/invocation/models/PojoImpl.java        |  9 ++++--
 4 files changed, 61 insertions(+), 13 deletions(-)

diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerEnvironment.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerEnvironment.java
index 4206363..2dc2359 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerEnvironment.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerEnvironment.java
@@ -180,11 +180,19 @@ public class SwaggerEnvironment {
   private Map<String, Method> retrieveVisibleMethods(Class<?> clazz) {
     Map<String, Method> visibleMethods = new HashMap<>();
     for (Method method : clazz.getMethods()) {
-      if (method.isAnnotationPresent(ApiOperation.class) &&
-          method.getAnnotation(ApiOperation.class).hidden()) {
-        continue;
+      String methodName = method.getName();
+      ApiOperation apiOperationAnnotation = method.getAnnotation(ApiOperation.class);
+      if (apiOperationAnnotation != null) {
+        if (apiOperationAnnotation.hidden()) {
+          continue;
+        }
+
+        if (StringUtils.isNotEmpty(apiOperationAnnotation.nickname())) {
+          methodName = apiOperationAnnotation.nickname();
+        }
       }
-      visibleMethods.put(method.getName(), method);
+
+      visibleMethods.put(methodName, method);
     }
     return visibleMethods;
   }
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
index 10dedfb..339eec2 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
@@ -18,6 +18,7 @@ package io.servicecomb.swagger.engine;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.concurrent.CompletableFuture;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -105,12 +106,43 @@ public class SwaggerProducerOperation {
   }
 
   public void invoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
-    ContextUtils.setInvocationContext(invocation);
+    if (CompletableFuture.class.equals(producerMethod.getReturnType())) {
+      completableFutureInvoke(invocation, asyncResp);
+      return;
+    }
 
-    Response response = doInvoke(invocation);
+    syncInvoke(invocation, asyncResp);
+  }
 
+  public void completableFutureInvoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
+    ContextUtils.setInvocationContext(invocation);
+    doCompletableFutureInvoke(invocation, asyncResp);
     ContextUtils.removeInvocationContext();
+  }
+
+  @SuppressWarnings("unchecked")
+  public void doCompletableFutureInvoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
+    try {
+      Object[] args = argumentsMapper.toProducerArgs(invocation);
+      Object result = producerMethod.invoke(producerInstance, args);
+
+      ((CompletableFuture<Object>) result).whenComplete((realResult, ex) -> {
+        if (ex == null) {
+          asyncResp.handle(responseMapper.mapResponse(invocation.getStatus(), realResult));
+          return;
+        }
 
+        asyncResp.handle(processException(ex));
+      });
+    } catch (Throwable e) {
+      asyncResp.handle(processException(e));
+    }
+  }
+
+  public void syncInvoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
+    ContextUtils.setInvocationContext(invocation);
+    Response response = doInvoke(invocation);
+    ContextUtils.removeInvocationContext();
     asyncResp.handle(response);
   }
 
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
index ff3bcc3..2047505 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
@@ -77,15 +77,18 @@ public class LocalProducerInvoker implements InvocationHandler {
     SwaggerProducerOperation producerOp = producer.findOperation(consumerOp.getSwaggerMethod().getName());
 
     consumerOp.getArgumentsMapper().toInvocation(args, invocation);
-    producerResponse = producerOp.doInvoke(invocation);
-    Object realResult = consumerOp.getResponseMapper().mapResponse(producerResponse);
 
-    if (CompletableFuture.class.equals(method.getReturnType())) {
-      CompletableFuture<Object> future = new CompletableFuture<>();
+    CompletableFuture<Object> future = new CompletableFuture<>();
+    producerOp.invoke(invocation, ar -> {
+      producerResponse = ar;
+      Object realResult = consumerOp.getResponseMapper().mapResponse(producerResponse);
       future.complete(realResult);
+    });
+
+    if (CompletableFuture.class.equals(method.getReturnType())) {
       return future;
     }
 
-    return realResult;
+    return future.get();
   }
 }
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/models/PojoImpl.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/models/PojoImpl.java
index 87a83e5..d772b16 100644
--- a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/models/PojoImpl.java
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/models/PojoImpl.java
@@ -19,8 +19,10 @@ package io.servicecomb.swagger.invocation.models;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 import io.servicecomb.swagger.invocation.context.InvocationContext;
+import io.swagger.annotations.ApiOperation;
 
 public class PojoImpl {
   public int testSimple(int a, int b, int c) {
@@ -32,8 +34,11 @@ public class PojoImpl {
     return user;
   }
 
-  public String testSimpleAndObject(String prefix, Person user) {
-    return prefix + " " + user.getName();
+  @ApiOperation(nickname = "testSimpleAndObject", value = "")
+  public CompletableFuture<String> testSimpleAndObjectAsync(String prefix, Person user) {
+    CompletableFuture<String> future = new CompletableFuture<>();
+    future.complete(prefix + " " + user.getName());
+    return future;
   }
 
   public String testContext(InvocationContext context, String name) {

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.