You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2017/12/27 09:14:43 UTC

[GitHub] liubao68 closed pull request #470: [JAV-592] Producer return completable future

liubao68 closed pull request #470: [JAV-592] Producer return completable future
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/470
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClient.java b/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClient.java
index 578c722b0..a3ef60c03 100644
--- a/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClient.java
+++ b/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClient.java
@@ -152,7 +152,11 @@ protected void testCodeFirstIsTrue(CodeFirstPojoIntf codeFirst) {
   }
 
   protected void testCodeFirstSayHi2(CodeFirstPojoIntf codeFirst) {
-    String result = codeFirst.sayHi2("world");
+    if (!CodeFirstPojoClientIntf.class.isInstance(codeFirst)) {
+      return;
+    }
+
+    String result = ((CodeFirstPojoClientIntf) codeFirst).sayHi2("world");
     TestMgr.check("world sayhi 2", result);
   }
 
diff --git a/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClientIntf.java b/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClientIntf.java
index 6603deb8c..15b91e926 100644
--- a/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClientIntf.java
+++ b/demo/demo-pojo/pojo-client/src/main/java/io/servicecomb/demo/pojo/client/CodeFirstPojoClientIntf.java
@@ -24,4 +24,6 @@
 public interface CodeFirstPojoClientIntf extends CodeFirstPojoIntf {
   @ApiOperation(nickname = "sayHi", value = "")
   CompletableFuture<String> sayHiAsync(String name);
+
+  String sayHi2(String name);
 }
diff --git a/demo/demo-pojo/pojo-server/src/main/java/io/servicecomb/demo/pojo/server/CodeFirstPojo.java b/demo/demo-pojo/pojo-server/src/main/java/io/servicecomb/demo/pojo/server/CodeFirstPojo.java
index 4a68d9ab6..bf7588288 100644
--- a/demo/demo-pojo/pojo-server/src/main/java/io/servicecomb/demo/pojo/server/CodeFirstPojo.java
+++ b/demo/demo-pojo/pojo-server/src/main/java/io/servicecomb/demo/pojo/server/CodeFirstPojo.java
@@ -20,12 +20,14 @@
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.CompletableFuture;
 
 import io.servicecomb.demo.CodeFirstPojoIntf;
 import io.servicecomb.demo.compute.Person;
 import io.servicecomb.demo.server.User;
 import io.servicecomb.provider.pojo.RpcSchema;
 import io.servicecomb.swagger.invocation.context.ContextUtils;
+import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.SwaggerDefinition;
 
 @RpcSchema()
@@ -85,8 +87,11 @@ public String sayHi(String name) {
     return name + " sayhi";
   }
 
-  public String sayHi2(String name) {
-    return name + " sayhi 2";
+  @ApiOperation(nickname = "sayHi2", value = "")
+  public CompletableFuture<String> sayHi2Async(String name) {
+    CompletableFuture<String> future = new CompletableFuture<>();
+    future.complete(name + " sayhi 2");
+    return future;
   }
 
   public boolean isTrue() {
diff --git a/demo/demo-schema/src/main/java/io/servicecomb/demo/CodeFirstPojoIntf.java b/demo/demo-schema/src/main/java/io/servicecomb/demo/CodeFirstPojoIntf.java
index 80ce57b27..a8749bd03 100644
--- a/demo/demo-schema/src/main/java/io/servicecomb/demo/CodeFirstPojoIntf.java
+++ b/demo/demo-schema/src/main/java/io/servicecomb/demo/CodeFirstPojoIntf.java
@@ -43,8 +43,6 @@
 
   String sayHi(String name);
 
-  String sayHi2(String name);
-
   boolean isTrue();
 
   String addString(List<String> s);
diff --git a/integration-tests/pojo-test/src/test/java/io/servicecomb/demo/pojo/test/PojoIntegrationTestBase.java b/integration-tests/pojo-test/src/test/java/io/servicecomb/demo/pojo/test/PojoIntegrationTestBase.java
index e5f4b5add..4f4c976f9 100644
--- a/integration-tests/pojo-test/src/test/java/io/servicecomb/demo/pojo/test/PojoIntegrationTestBase.java
+++ b/integration-tests/pojo-test/src/test/java/io/servicecomb/demo/pojo/test/PojoIntegrationTestBase.java
@@ -303,12 +303,6 @@ public void remoteCodeFirstPojo_sayHi() {
     assertThat(result, is("world sayhi"));
   }
 
-  @Test
-  public void remoteCodeFirstPojo_sayHi2() {
-    String result = PojoService.codeFirst.sayHi2("world");
-    assertThat(result, is("world sayhi 2"));
-  }
-
   @Test
   public void remoteCodeFirstPojo_isTrue() {
     boolean result = PojoService.codeFirst.isTrue();
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 4206363bb..2dc23597e 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 SwaggerProducer createProducer(Object producerInstance) {
   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 10dedfbd9..339eec28d 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 @@
 
 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 void setResponseMapper(ProducerResponseMapper responseMapper) {
   }
 
   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 ff3bcc361..20475057f 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 Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
     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 87a83e580..d772b16b6 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 @@
 
 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 Person testObject(Person user) {
     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) {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services