不使用 xml 文件的 Spring Integration 中的日志记录过程 [英] Logging process in Spring Integration without using xml files

查看:37
本文介绍了不使用 xml 文件的 Spring Integration 中的日志记录过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在不使用 xml 文件进行配置的情况下记录进程的时间.通过阅读其他帖子,我想出了丰富集成流程中的标题.这有点工作,但不是为了正确的目的.对于每个新启动的进程,它在应用程序启动时给我一个 startTime(即一个常量).见下文:

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

我的目标是正确记录进程而不使用我上面所说的 xml 文件,但我无法做到这一点.我找到了一个例子并尝试了一个像这样的 ChannelInterceptorAdapter 的解决方案:

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?

推荐答案

.enrichHeaders(h -> h.header("startTime", String.valueOf(System.currentTimeMillis())))属于这个:

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

注意StaticHeaderValueMessageProcessor.所以,你展示的东西真的是一个常数.

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

如果您需要为要处理的每条消息计算一个值,您应该考虑使用基于 Function 的变体:

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

这篇关于不使用 xml 文件的 Spring Integration 中的日志记录过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆