[
https://issues.apache.org/jira/browse/SCB-960?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16649712#comment-16649712
]
ASF GitHub Bot commented on SCB-960:
------------------------------------
wujimin opened a new pull request #954: [SCB-960] get context from future
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/954
https://issues.apache.org/jira/browse/SCB-960
----------------------------------------------------------------
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)