You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/10/15 11:28:15 UTC

[GitHub] liubao68 closed pull request #954: [SCB-960] get context from future

liubao68 closed pull request #954: [SCB-960] get context from future
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/954
 
 
   

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/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 bcb444810..21dfa8d17 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.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 @@ protected Object syncInvoke(Invocation invocation, SwaggerConsumerOperation cons
 
   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 ec7b29270..f391cda2c 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.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 @@ void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
       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 36fca8c04..4f6430ea9 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 static void setInvocationContext(InvocationContext invocationContext) {
   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/InvocationContextCompletableFuture.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContextCompletableFuture.java
new file mode 100644
index 000000000..222eb5d06
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContextCompletableFuture.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.swagger.invocation.context;
+
+import java.util.concurrent.CompletableFuture;
+
+public class InvocationContextCompletableFuture<T> extends CompletableFuture<T> {
+  private InvocationContext context;
+
+  public InvocationContextCompletableFuture(InvocationContext context) {
+    this.context = context;
+  }
+
+  public InvocationContext getContext() {
+    return context;
+  }
+}
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java
new file mode 100644
index 000000000..c84b4dcde
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestContextUtils.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.swagger.invocation.context;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestContextUtils {
+  @Test
+  public void getFromCompletableFuture() {
+    Assert.assertNull(ContextUtils.getFromCompletableFuture(new CompletableFuture<>()));
+
+    InvocationContext context = new InvocationContext();
+    Assert
+        .assertSame(context, ContextUtils.getFromCompletableFuture(new InvocationContextCompletableFuture<>(context)));
+  }
+}


 

----------------------------------------------------------------
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