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:41 UTC

[incubator-servicecomb-java-chassis] branch master updated (b15551c -> 4ab3571)

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

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


    from b15551c  JAV-583 add demo for different consumer method name
     new acb84cf  JAV-592 producer support return CompletableFuture, and support different method name between swagger and producer
     new 4ab3571  JAV-592 add demo for producer return CompletableFuture

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../demo/pojo/client/CodeFirstPojoClient.java      |  6 +++-
 .../demo/pojo/client/CodeFirstPojoClientIntf.java  |  2 ++
 .../demo/pojo/server/CodeFirstPojo.java            |  9 ++++--
 .../io/servicecomb/demo/CodeFirstPojoIntf.java     |  2 --
 .../demo/pojo/test/PojoIntegrationTestBase.java    |  6 ----
 .../swagger/engine/SwaggerEnvironment.java         | 16 +++++++---
 .../swagger/engine/SwaggerProducerOperation.java   | 36 ++++++++++++++++++++--
 .../engine/unittest/LocalProducerInvoker.java      | 13 +++++---
 .../swagger/invocation/models/PojoImpl.java        |  9 ++++--
 9 files changed, 75 insertions(+), 24 deletions(-)

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

[incubator-servicecomb-java-chassis] 02/02: JAV-592 add demo for producer return CompletableFuture

Posted by li...@apache.org.
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 4ab35710f80bb043142f098aa7235357f27404c0
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Dec 26 14:35:40 2017 +0800

    JAV-592 add demo for producer return CompletableFuture
---
 .../io/servicecomb/demo/pojo/client/CodeFirstPojoClient.java     | 6 +++++-
 .../io/servicecomb/demo/pojo/client/CodeFirstPojoClientIntf.java | 2 ++
 .../main/java/io/servicecomb/demo/pojo/server/CodeFirstPojo.java | 9 +++++++--
 .../src/main/java/io/servicecomb/demo/CodeFirstPojoIntf.java     | 2 --
 .../io/servicecomb/demo/pojo/test/PojoIntegrationTestBase.java   | 6 ------
 5 files changed, 14 insertions(+), 11 deletions(-)

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 578c722..a3ef60c 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 @@ public class CodeFirstPojoClient {
   }
 
   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 6603deb..15b91e9 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 @@ import io.swagger.annotations.ApiOperation;
 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 4a68d9a..bf75882 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 @@ package io.servicecomb.demo.pojo.server;
 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 class CodeFirstPojo implements CodeFirstPojoIntf {
     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 80ce57b..a8749bd 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 @@ public interface CodeFirstPojoIntf {
 
   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 e5f4b5a..4f4c976 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
@@ -304,12 +304,6 @@ public class PojoIntegrationTestBase {
   }
 
   @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();
     assertThat(result, is(true));

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

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

Posted by li...@apache.org.
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>.