You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/12/13 02:13:38 UTC

[GitHub] gdouyang commented on issue #1031: 关于jackson自定Serializer中调用ContextUtils.getInvocationContext()返回null问题

gdouyang commented on issue #1031: 关于jackson自定Serializer中调用ContextUtils.getInvocationContext()返回null问题
URL: https://github.com/apache/servicecomb-java-chassis/issues/1031#issuecomment-446817056
 
 
   目前我的解决办法是写了一个HttpServerFilter,修改了beforeSendResponseAsync
   
   1. 系统内部做了校验可以排除InvocationContext没有初始化的情况;
   2. 目前系统都是java的同构语言;
   3. 业务都是使用rest调用;
   
   但不确定会不会出现执行线程不同的情况,请问一个这样的写法会不会出现执行线程不同?
   
   ```
   @Override
       public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
         Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
         ProduceProcessor produceProcessor =
             (ProduceProcessor) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
         Object body = response.getResult();
         if (response.isFailed()) {
           body = ((InvocationException) body).getErrorData();
         }
   
         if (Part.class.isInstance(body)) {
           return responseEx.sendPart((Part) body);
         }
   
         responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");
   
         CompletableFuture<Void> future = new CompletableFuture<>();
         try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
           ContextUtils.setInvocationContext(invocation); // 添加上下文
           produceProcessor.encodeResponse(output, body);
           ContextUtils.removeInvocationContext(); // 删除上下文
   
           responseEx.setBodyBuffer(output.getBuffer());
           future.complete(null);
         } catch (Throwable e) {
           future.completeExceptionally(ExceptionFactory.convertProducerException(e));
         }
         return future;
       }
   ```

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services