bringwu opened a new issue, #13666:
URL: https://github.com/apache/dubbo/issues/13666

   provider service method:
   
   ```
        public CompletableFuture<Integer> newToken(NewTokenInfo tReq) {
                TokenInfo tokenInfo = new TokenInfo(JsonUtils.toJson(tReq), 
60);        
                // 可以设置回应附加信息
   //           RpcContext.getServerResponseContext().setAttachment("testInfo", 
JsonUtils.toJson(tokenInfo));
                // 可以设置回应附加信息
   //           RpcContext.getServerContext().setAttachment("testInfo", 
JsonUtils.toJson(tokenInfo));
   
                CompletableFuture<Integer> completableFuture = new 
CompletableFuture<>();
                completableFuture = completableFuture.completeAsync(()->{
                        logger.debug("tokenInfo:" + 
JsonUtils.toJson(tokenInfo));
                        return 1;
                }, Executors.newFixedThreadPool(1));
                
                // 不可以设置回应附加信息
                RpcContextAttachment serverResponseContext = 
RpcContext.getServerResponseContext();
   //           RpcContextAttachment serverResponseContext = 
RpcContext.getClientAttachment();
   //           RpcContextAttachment serverResponseContext = 
RpcContext.getServerContext();
   //           RpcServiceContext serverResponseContext = 
RpcContext.getServiceContext();
   //           RpcServiceContext serverResponseContext = 
RpcContext.getCurrentServiceContext();
   //           RpcContextAttachment serverResponseContext = 
RpcContext.getClientResponseContext();
   //           RpcContextAttachment serverResponseContext = 
RpcContext.getServerAttachment();
                completableFuture = completableFuture.thenApply(ret->{
                        // 不可以设置回应附加信息
   //                   RpcContextAttachment serverResponseContext = 
RpcContext.getServerResponseContext();
   //                   RpcContextAttachment serverResponseContext = 
RpcContext.getClientAttachment();
   //                   RpcContextAttachment serverResponseContext = 
RpcContext.getServerContext();
   //                   RpcServiceContext serverResponseContext = 
RpcContext.getServiceContext();
   //                   RpcServiceContext serverResponseContext = 
RpcContext.getCurrentServiceContext();
   //                   RpcContextAttachment serverResponseContext = 
RpcContext.getClientResponseContext();
   //                   RpcContextAttachment serverResponseContext = 
RpcContext.getServerAttachment();
                        
                        serverResponseContext.setAttachment("testInfo", 
JsonUtils.toJson(tokenInfo));
                        return ret;
                });
                return completableFuture;
        }
   ```
   
   consumer :
   ```
                NewTokenInfo tReq = new NewTokenInfo("testType", "testToken");
                CompletableFuture<Integer> future = 
tokenRpcServant.newToken(tReq);
                CompletableFuture<Void> completableFuture = 
future.thenAccept(ret->{
                        logger.debug("newToken: " + ret + ", testInfo: " + 
RpcContext.getClientResponseContext().getAttachment("testInfo"));
                        logger.debug("newToken: " + ret + ", testInfo: " + 
RpcContext.getServerContext().getAttachment("testInfo"));
                });
                try {
                        completableFuture.get();
                } catch (Exception e) {
                        throw new RuntimeException("未知异常", e);
                }
   ```
   
   provider的方法是异步执行,异步执行完后才能往RpcContext设置回应附加信息,这种情况回应附加信息没法传递到consumer这边


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to