Logging process in Spring Integration without using xml files

岁酱吖の 提交于 2019-12-08 12:24:31

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/49924806/logging-process-in-spring-integration-without-using-xml-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!