[ 
https://issues.apache.org/jira/browse/SCB-960?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16650091#comment-16650091
 ] 

ASF GitHub Bot commented on SCB-960:
------------------------------------

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:
[email protected]


> when consumer local failed(eg: LB failed), CompletableFuture callback can not 
> get InvocationContext
> ---------------------------------------------------------------------------------------------------
>
>                 Key: SCB-960
>                 URL: https://issues.apache.org/jira/browse/SCB-960
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Java-Chassis
>            Reporter: wujimin
>            Assignee: wujimin
>            Priority: Major
>
> to support inherit context for all future scenes automatically, must wrap 
> arguments and return future as below codes.
>  and need to wrap about 40 methods
> {code:java}
> @Override
> public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? 
> extends U> fn) {
>   return wrap(super.handle(wrap(fn)));
> }
> public <T> CompletableFuture<T> wrap(CompletableFuture<T> baseFuture) {
>   InvocationContextCompletableFuture<T> future = new 
> InvocationContextCompletableFuture<>(context);
>   baseFuture.whenComplete((r, e) -> {
>     if (e == null) {
>       future.complete(r);
>       return;
>     }
>     future.completeExceptionally(e);
>   });
>   return future;
> }
> public <T, R> Function<T, R> wrap(Function<T, R> action) {
>   return (t) -> {
>     InvocationContext old = ContextUtils.getInvocationContext();
>     ContextUtils.setInvocationContext(context);
>     try {
>       return action.apply(t);
>     } finally {
>       ContextUtils.setInvocationContext(old);
>     }
>   };
> }
> {code}
> it's too complex.
>  so we will only add a api to get context from future,that's enough.
>  consumers get context from future and then choose one of the solution:
> 1.threadLocal, need to clear
> {code:java}
> ContextUtils.setInvocationContext(context);
> try {
>   // business logic
> } finally {
>   ContextUtils.removeInvocationContext();
> }
> {code}
> 2.rpc: context to be a argument of the consumer api
> {code:java}
> String testContext(InvocationContext context, String name);
> {code}
> 3.restTemplate: use CseHttpEntity
> {code:java}
> CseHttpEntity<?> httpEntity = new CseHttpEntity<>(body);
> httpEntity.setContext(context);
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to