Fatmajk
Fatmajk

Reputation: 2009

Logging process in Spring Integration without using xml files

My goal here is to log time of a process without using xml files for configurations. By reading other posts I came up with enriching headers in the integration flow. This kinda works, but not for the right purpose. For every new started process it gives me a startTime when the application is launched (i.e. a constant). See below:

@Bean
public IntegrationFlow processFileFlow() {
    return IntegrationFlows
        .from(FILE_CHANNEL_PROCESSING)
        .transform(fileToStringTransformer())
        .enrichHeaders(h -> h.header("startTime", String.valueOf(System.currentTimeMillis())))
        .handle(FILE_PROCESSOR, "processFile").get();
}

My goal is to properly log the process without using xml files like I said above but I don't manage to do this. I found an example and tried a solution with ChannelInterceptorAdapter like this:

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

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

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

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

}

But I receive no logs at all. Any ideas?

Upvotes: 0

Views: 58

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121202

The .enrichHeaders(h -> h.header("startTime", String.valueOf(System.currentTimeMillis()))) falls to this:

public <V> HeaderEnricherSpec header(String name, V value, Boolean overwrite) {
    AbstractHeaderValueMessageProcessor<V> headerValueMessageProcessor =
            new StaticHeaderValueMessageProcessor<>(value);
    headerValueMessageProcessor.setOverwrite(overwrite);
    return header(name, headerValueMessageProcessor);
}

Pay attention to the StaticHeaderValueMessageProcessor. So, what you show is really a constant.

If you need a value calculated for each message to process, you should consider to use Function-based variant:

.enrichHeaders(h -> 
               h.headerFunction("startTime", 
                        m -> String.valueOf(System.currentTimeMillis())))

Upvotes: 1

Related Questions