[ https://issues.apache.org/jira/browse/SCB-960?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
wujimin updated SCB-960: ------------------------ Description: 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} > 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)