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 2018/10/15 11:28:18 UTC

[incubator-servicecomb-java-chassis] branch master updated: [SCB-960] get context from future

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


The following commit(s) were added to refs/heads/master by this push:
     new abdb21a  [SCB-960] get context from future
abdb21a is described below

commit abdb21adee72717f8fadd6f5fa2a89ebe4e00920
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Oct 15 12:35:49 2018 +0800

    [SCB-960] get context from future
---
 .../apache/servicecomb/provider/pojo/Invoker.java  |  3 +-
 .../servicecomb/provider/pojo/TestInvoker.java     |  3 ++
 .../swagger/invocation/context/ContextUtils.java   | 15 ++++++++++
 ...ava => InvocationContextCompletableFuture.java} | 30 +++++--------------
 .../invocation/context/TestContextUtils.java}      | 35 +++++++---------------
 5 files changed, 37 insertions(+), 49 deletions(-)

diff --git a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
index bcb4448..21dfa8d 100644
--- a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
+++ b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
@@ -33,6 +33,7 @@ import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.swagger.engine.SwaggerConsumer;
 import org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation;
 import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContextCompletableFuture;
 import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
 import org.springframework.util.StringUtils;
 
@@ -168,7 +169,7 @@ public class Invoker implements InvocationHandler {
 
   protected CompletableFuture<Object> completableFutureInvoke(Invocation invocation,
       SwaggerConsumerOperation consumerOperation) {
-    CompletableFuture<Object> future = new CompletableFuture<>();
+    CompletableFuture<Object> future = new InvocationContextCompletableFuture<>(invocation);
     InvokerUtils.reactiveInvoke(invocation, response -> {
       if (response.isSuccessed()) {
         Object result = consumerOperation.getResponseMapper().mapResponse(response);
diff --git a/providers/provider-pojo/src/test/java/org/apache/servicecomb/provider/pojo/TestInvoker.java b/providers/provider-pojo/src/test/java/org/apache/servicecomb/provider/pojo/TestInvoker.java
index ec7b292..f391cda 100644
--- a/providers/provider-pojo/src/test/java/org/apache/servicecomb/provider/pojo/TestInvoker.java
+++ b/providers/provider-pojo/src/test/java/org/apache/servicecomb/provider/pojo/TestInvoker.java
@@ -34,6 +34,7 @@ import org.apache.servicecomb.swagger.engine.SwaggerConsumerOperation;
 import org.apache.servicecomb.swagger.engine.bootstrap.BootstrapNormal;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContextCompletableFuture;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper;
 import org.hamcrest.Matchers;
@@ -203,6 +204,8 @@ public class TestInvoker {
       Assert.assertEquals(1, result);
       Assert.assertEquals(null, ex);
     });
+
+    Assert.assertThat(future, Matchers.instanceOf(InvocationContextCompletableFuture.class));
   }
 
   @Test
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
index 36fca8c..4f6430e 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
@@ -17,6 +17,8 @@
 
 package org.apache.servicecomb.swagger.invocation.context;
 
+import java.util.concurrent.CompletableFuture;
+
 /**
  * 传递调用过程的上下文数据
  */
@@ -45,4 +47,17 @@ public final class ContextUtils {
   public static void removeInvocationContext() {
     contextMgr.remove();
   }
+
+  /**
+   *
+   * @param future must be InvocationContextCompletableFuture, that is returned from consumer api
+   * @return
+   */
+  public static InvocationContext getFromCompletableFuture(CompletableFuture<?> future) {
+    if (future instanceof InvocationContextCompletableFuture) {
+      return ((InvocationContextCompletableFuture<?>) future).getContext();
+    }
+
+    return null;
+  }
 }
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContextCompletableFuture.java
similarity index 57%
copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
copy to swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContextCompletableFuture.java
index 36fca8c..222eb5d 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContextCompletableFuture.java
@@ -17,32 +17,16 @@
 
 package org.apache.servicecomb.swagger.invocation.context;
 
-/**
- * 传递调用过程的上下文数据
- */
-public final class ContextUtils {
-  private ContextUtils() {
-  }
+import java.util.concurrent.CompletableFuture;
 
-  private static ThreadLocal<InvocationContext> contextMgr = new ThreadLocal<>();
+public class InvocationContextCompletableFuture<T> extends CompletableFuture<T> {
+  private InvocationContext context;
 
-  public static InvocationContext getInvocationContext() {
-    return contextMgr.get();
+  public InvocationContextCompletableFuture(InvocationContext context) {
+    this.context = context;
   }
 
-  public static InvocationContext getAndRemoveInvocationContext() {
-    InvocationContext context = contextMgr.get();
-    if (context != null) {
-      contextMgr.remove();
-    }
+  public InvocationContext getContext() {
     return context;
   }
-
-  public static void setInvocationContext(InvocationContext invocationContext) {
-    contextMgr.set(invocationContext);
-  }
-
-  public static void removeInvocationContext() {
-    contextMgr.remove();
-  }
-}
+}
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java
similarity index 55%
copy from swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
copy to swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java
index 36fca8c..c84b4dc 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/ContextUtils.java
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java
@@ -14,35 +14,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.servicecomb.swagger.invocation.context;
 
-/**
- * 传递调用过程的上下文数据
- */
-public final class ContextUtils {
-  private ContextUtils() {
-  }
-
-  private static ThreadLocal<InvocationContext> contextMgr = new ThreadLocal<>();
+import java.util.concurrent.CompletableFuture;
 
-  public static InvocationContext getInvocationContext() {
-    return contextMgr.get();
-  }
-
-  public static InvocationContext getAndRemoveInvocationContext() {
-    InvocationContext context = contextMgr.get();
-    if (context != null) {
-      contextMgr.remove();
-    }
-    return context;
-  }
+import org.junit.Assert;
+import org.junit.Test;
 
-  public static void setInvocationContext(InvocationContext invocationContext) {
-    contextMgr.set(invocationContext);
-  }
+public class TestContextUtils {
+  @Test
+  public void getFromCompletableFuture() {
+    Assert.assertNull(ContextUtils.getFromCompletableFuture(new CompletableFuture<>()));
 
-  public static void removeInvocationContext() {
-    contextMgr.remove();
+    InvocationContext context = new InvocationContext();
+    Assert
+        .assertSame(context, ContextUtils.getFromCompletableFuture(new InvocationContextCompletableFuture<>(context)));
   }
 }