代码之家  ›  专栏  ›  技术社区  ›  Makky

弹簧集成错误正在连接完成的有效负载

  •  0
  • Makky  · 技术社区  · 7 年前

    public IntegrationFlow queueProcessorFlow() {
            return IntegrationFlows.from(Jms.inboundAdapter(jmsTemplate)
    
                            .destination("test_queue"),
                    c -> c.poller(Pollers.fixedDelay(5000L)
                            .maxMessagesPerPoll(1)))
    
                    //convert json to our custom object
                    .transform(new JsonToQueueEventConverterTransformer(springBeanFactory))
    
                    .transform(new CustomTransformer(springBeanFactory))
    
    
                    .handle(o -> {
    
    
                    }).get();
        }
    

    变压器

    public class CustomerTransformer implements GenericTransformer<CustomPojo, CustomPojo> {
    
    
        private final QueueDataProcessorSpringBeanFactory factory;
    
    
        @Override
        public CustomPojo transform(CustomPojo CustomPojo) {
            try {
    
               //do something e.g. service call
               throw new Exception("This failed mate !! SOS");
            } catch (Exception e) {            
    
            //ISSUE here 
            //e contains the original payload in the stack trace 
                throw new RuntimeException(e);
            }
            return CustomPojo;
    
        }
    

    现在,当我抛出自定义异常时,堆栈跟踪包含所有内容。它甚至包含有效载荷。我对异常情况下的有效载荷不感兴趣。

    如何更新以不包括有效负载?

    ** 更新 **

    org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message; nested exception is org.springframework.messaging.MessageHandlingException: nested exception is org.springframework.integration.transformer.MessageTransformationException: Error initiliazing the :; nested exception is CustomException Error lab lab lab  , failedMessage=GenericMessage [payload=
    

    我的错误处理程序

     @Bean
        public IntegrationFlow errorHandlingFlow() {
            return IntegrationFlows.from("errorChannel")
                    .handle(message -> {
                        try {
    
                            ErrorMessage e = (ErrorMessage) message;
                            if (e.getPayload() instanceof MessageTransformationException) {
                                String stackTrace = ExceptionUtils.getStackTrace(e.getPayload());
                                LOG.info("Exception trace {} ", stackTrace);
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Artem Bilan    7 年前

    不确定失去一家公司的商业目的是什么 payload 在堆栈跟踪中,但您可以通过 MessageTransformationException RuntimeException

    要避免堆栈跟踪中出现具有上述有效负载的消息,您需要使用以下构造函数之一:

    public MessageTransformationException(String description, Throwable cause) {
        super(description, cause);
    }
    
    public MessageTransformationException(String description) {
        super(description);
    }
    

    Message<?> .

    MessageTransformingHandler

    protected Object handleRequestMessage(Message<?> message) {
        try {
            return this.transformer.transform(message);
        }
        catch (Exception e) {
            if (e instanceof MessageTransformationException) {
                throw (MessageTransformationException) e;
            }
            throw new MessageTransformationException(message, "Failed to transform Message", e);
        }
    }
    

    更新

    原来 AbstractMessageHandler 检查 MessageHandlingException 用于包装 IntegrationUtils.wrapInHandlingExceptionIfNecessary() . 因此,我建议抛出一个 MessageHandlingException 而是从您的代码中。并将此构造函数与 null 对于消息arg:

    MessageHandlingException(Message<?> failedMessage, Throwable cause)
    
        2
  •  0
  •   Miguel Galindo    5 年前

    我也有同样的问题,也许这能帮到你。如果使用默认值 errorChannel 这个Bean已经订阅了一个 LoggingHandler 错误通道 通过这种方式,您将覆盖默认行为

        @Bean
        @Qualifier(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)
        public MessageChannel errorChannel() {
            return new PublishSubscribeChannel();
        }
    

    如果您的问题是在使用 .log() 处理程序您始终可以使用函数来决定要显示消息的哪一部分

      @Bean
      public IntegrationFlow errorFlow(IntegrationFlow 
        createOutFileInCaseErrorFlow) {
        return 
        IntegrationFlows.from(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)
       .log(LoggingHandler.Level.ERROR, m -> m.getHeaders())
       .<MessagingException>log(Level.ERROR, p -> p.getPayload().getMessage())
       .get();
      }