有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring集成错误正在附加已完成的负载

我有一个JMS的侦听器。一旦我阅读了消息,我就转换成我的自定义对象

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);

共 (2) 个答案

  1. # 1 楼答案

    我也有同样的问题,也许这能帮到你。如果您使用默认的errorChannelBean,它已经订阅了一个LoggingHandler来打印完整的消息,如果您想避免打印负载,您可以通过这种方式创建自己的errorChannel,您将覆盖默认行为

        @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();
      }
    
  2. # 2 楼答案

    不确定在堆栈跟踪中丢失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);
        }
    }
    

    更新

    结果证明MessageTransformationException是不够的,因为AbstractMessageHandler检查MessageHandlingException以在IntegrationUtils.wrapInHandlingExceptionIfNecessary()中进行包装。因此,我建议从代码中抛出一个MessageHandlingException。并将此构造函数与消息arg的null一起使用:

    MessageHandlingException(Message<?> failedMessage, Throwable cause)