Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
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
@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); } }; }
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
ContextUtils.setInvocationContext(context); try { // business logic } finally { ContextUtils.removeInvocationContext(); }
2.rpc: context to be a argument of the consumer api
String testContext(InvocationContext context, String name);
3.restTemplate: use CseHttpEntity
CseHttpEntity<?> httpEntity = new CseHttpEntity<>(body);
httpEntity.setContext(context);
Attachments
Issue Links
- links to