java - Spring Integration 错误附加已完成的有效负载

标签 java spring-integration spring-integration-dsl

我有一个 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);

最佳答案

不确定失去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从你的代码代替。并将此构造函数与 null 一起使用对于消息参数:

MessageHandlingException(Message<?> failedMessage, Throwable cause)

关于java - Spring Integration 错误附加已完成的有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596124/

相关文章:

java - Spring Cloud 流: Republish to other amqp connection if current connection throws exception

java - @PreUpdate 的使用问题

java - 用 PDFBox 替换 pdf 中 TrueType(CID) 编码的 Identity-H 字符串

java - 带有外部 Web 服务监控的 Spring Integration 应用程序

spring-boot - 如何在 Spring 集成中重试对 Webflux.outboundgateway 的失败调用

java - 将自定义对象传递给 HttpRequestExecutingMessageHandler 时,Spring Integration 406 null

spring-integration - 如何将请求 header 添加到 outboundGateway spring 集成 dsl

javascript - 8 :00AM on specific date when the App got completely closed? 如何通知用户

java - Spring Boot 与 GraphQL - 架构问题

spring-integration - Spring Cloud Stream有没有SNS模块