0

As part of non-functional requirements, I have to log the response time for each http-outbound calls in my spring integration flow.

I have a series of http:outbound-gateway's which make REST API calls (request/response in JSON). I have to log different things like request payload, service endpoint name, status (success/failure).

I have tried to use ChannelInterceptorAdapter like:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
import org.springframework.stereotype.Component;

@Component(value = "integrationLoggingInterceptor")
public class IntegrationLoggingInterceptor extends ChannelInterceptorAdapter  {

    private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationLoggingInterceptor.class);

    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        LOGGER.debug("Post Send - Channel " + channel.getClass());
        LOGGER.debug("Post Send - Headers: " + message.getHeaders() + " Payload: " + message.getPayload() + " Message sent?: " + sent);
    }

    @Override
    public Message<?> postReceive(Message<?> message, MessageChannel channel) {
        try {
            LOGGER.debug("Post Receive - Channel " + channel.getClass());
            LOGGER.debug("Post Receive - Headers: " + message.getHeaders() + " Payload: " + message.getPayload());
        } catch(Exception ex) {
            LOGGER.error("Error in post receive : ", ex);
        }
        return message;
    }

}

But I am not able to figure out how to get response times for each http:outbound-gateway.

Any pointers/suggestions/hints/advice would be highly appreciated.

1 Answers1

1

Right, you should write Interceptor but not for channel and for <int-http:outbound-gateway>. There is a feature <request-handler-advice-chain>

It can be any implementation of AOP Advice. For your purpose the MethodInterceptor is a good choice. The advice is applied for AbstractReplyProducingMessageHandler.handleRequestMessage, where the real 'hard' work is done.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118