Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message headers not included in error handling with Spring Integration DSL

I am trying to track all transactions adding extra headers on each operation, these extra headers work fine with request and response, but in error case no headers are included.

This is my configuration (with Spring Integration DSL and Java 1.7)

@Bean
public IntegrationFlow inboundFlow() {
    return IntegrationFlows.from(Amqp.inboundGateway(simpleMessageListenerContainer())
        .mappedReplyHeaders(AMQPConstants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN)
        .mappedRequestHeaders(AMQPConstants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN)
        .errorChannel(gatewayErrorChannel())
        .requestChannel(gatewayRequestChannel())
        .replyChannel(gatewayResponseChannel())
    )
    .transform(getCustomFromJsonTransformer())
    .route(new HeaderValueRouter(AMQPConstants.OPERATION_ROUTING_KEY))
    .get();
}

@Bean
public MessageChannel gatewayRequestChannel() {
    return MessageChannels.direct().get();
}

@Bean
public MessageChannel gatewayResponseChannel() {
    return MessageChannels.publishSubscribe().get();
}

@Bean
public MessageChannel gatewayErrorChannel() {
    return MessageChannels.publishSubscribe().get();
}

@Bean       
public IntegrationFlow responseTrackerOutboundFlow() {      
    return trackerOutboundFlowTemplate(gatewayResponseChannel());       
}

@Bean       
public IntegrationFlow errorTrackerOutboundFlow() {     
    return trackerOutboundFlowTemplate(gatewayErrorChannel());      
}

private IntegrationFlow trackerOutboundFlowTemplate(MessageChannel fromMessageChannel) {        
    return IntegrationFlows.from(fromMessageChannel)        
        .handle(Amqp.outboundAdapter(new RabbitTemplate(getConnectionFactory()))
                .exchangeName(LOGGER_EXCHANGE_NAME)
                .routingKey(LOGGER_EXCHANGE_ROUTING_KEY)
                .mappedRequestHeaders("*"))     
         .get();        
}

I am using errorChannel for inboundGateway and also using mappedReplyHeaders and mappedRequestHeaders, is it possible to have headers in errorChannel? There is a way to configure mapped error headers or something like that?

like image 652
jcastaneyra Avatar asked Dec 21 '25 22:12

jcastaneyra


1 Answers

The mappedReplyHeaders work only if you receive the good reply from the downstream flow. They are applied exactly before sending the reply message to the AMQP.

The errorChannel is a part of integration messaging, therefore no access to the mappedReplyHeaders at all. Forget them here!

From other side the errorChannel is responsible to wrap an Exception into the new ErrorMessage. That's why you don't see your headers there directly.

But you should bare in mind that integration messaging in most cases is MessagingException with the failedMessage property. That failedMessage is a "guilty" message for an exception.

And if the normal headers population process is done everywhere, you can get access to your headers from this failedMessage of the MessagingException payload of the ErrorMessage in the errorChannel flow.

like image 170
Artem Bilan Avatar answered Dec 23 '25 17:12

Artem Bilan