Spring Integration:如何拦截所有 spring 集成组件并获取要记录的属性值 [英] Spring Integration : How to intercept ALL spring integration component and fetch attribute values to log

查看:31
本文介绍了Spring Integration:如何拦截所有 spring 集成组件并获取要记录的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时拦截所有 Spring Integration 组件,并且应该能够获取属性值以记录有意义的消息.例如:

I need to intercept ALL the Spring Integration components at runtime and should be able to fetch the attribute values in order to log a meaningful message. For example:

<int-http:outbound-gateway url="someURL" http-method="GET"
                           request-channel="channel1"
                           expected-response-type="com.example.Test"
                           message-converters="customMessageConverters">
    <int-http:uri-variable name="testId" expression="headers.testId"/>
</int-http:outbound-gateway>

在上面的示例中,我需要拦截 int-http:outbound-gateway 并捕获 url、request-channel 和 expected-response-type 的值.我们需要为所有 http 出站网关执行此操作.

In the example above, I need to intercept int-http:outbound-gateway and capture the value for url, request-channel and expected-response-type. We need to do this for all http outbound gateway.

同样,对于所有其他组件,如 int-http:inbound-gateway、int-http:inbound-channel-adapter、int:transformer、int:header-enricher、int:chain、int:router 等.

Similarly, for all other components like int-http:inbound-gateway, int-http:inbound-channel-adapter, int:transformer, int:header-enricher, int:chain, int:router, etc.

我尝试创建一个实现 BeanPostProcessor - postProcessAfterInitialization 方法的自定义类.检查 bean 名称是否与组件匹配,并尝试检索所有详细信息,但 bean 已创建,并且此方法在服务器启动时调用.我的要求是在用户导航和调用任何特定路线时捕获流.此外,除了以下内容之外,我无法找到所有组件的 Java 类名.剩下的还在找.org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway for int-http:inbound-gateway, org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler for int-http:outbound-gateway

I have tried creating a custom class implementing BeanPostProcessor - postProcessAfterInitialization method. Checked for the bean name to be matching with the component, and tried to retrieve all the details but the beans are created and this method is called at the server startup itself. My requirement is to capture the flow as and when the user navigates and any particular route is being called. Also I am not able to find Java class name for all the component apart from the below. Still finding for the rest. org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway for int-http:inbound-gateway, org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler for int-http:outbound-gateway

更新:我已尝试以下操作,但在日志中看不到任何关于消息历史记录的额外输出.上面的代码有没有遗漏?

Update: I have tried the below but cant see any extra output in logs with respect to message history. Is anything missing in the above code?

<int:message-history />
<int:logging-channel-adapter id="logging"
    log-full-message="true" logger-name="message.history" level="DEBUG"/>
    <int:wire-tap pattern="*" order="3" channel="logging" /> 

<int:message-history /> 
    <int:logging-channel-adapter id="logger"
    log-full-message="true" logger-name="message.history" level="DEBUG"/>
<int:channel id="wiretapChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>  

此外,我试图将 LogMessage 注入到窃听中,以便从 MessageHistory 数据执行一些额外的任务.但是控件没有进入handleMessage方法.请帮忙.

Also, I am trying to inject LogMessage into wire-tap inorder to perform some additional tasks from MessageHistory data. But the control doesn't enter handleMessage method. Please help.

<bean id="logMessage" class="com.logging.LogMessage"/> 
<int:service-activator input-channel="wiretapChannel" ref="logMessage" method="handleMessage"></int:service-activator>

public class LogMessage {
public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException {
MessageHistory history = MessageHistory.read(message);
for (int i = 0; i < history.size(); i++) {
    Properties properties = history.get(i);
    getLogger().info("history: " + properties.get("name"));
}    
}
}

推荐答案

嗯,这不是那么标准的任务,尤其是对于您想要打印的阅读属性.其中更多的是基于 SpEL 表达式,实际值取决于请求消息.

Well, it's not so standard task, especially for the reading properties you would like to print. More over many of them are based on the SpEL expressions and the actual value depends on the request message.

虽然有一个非常接近您想要获得的组件.它被称为 Message历史.

There is though a component which is very close what you would like to get. It is called Message History.

您所需要的只是 - 所有 Spring Integration 组件都将被跟踪并将它们指向MessageHistory.HEADER_NAME 以显示整个集成流程中的消息路径.

All what you need is <int:message-history/> - and all the Spring Integration components will be tracked and will store they point into the MessageHistory.HEADER_NAME to show the whole path of the message over integration flow.

另外我通常也使用这样的东西:

In addition I usually also use something like this:

<wire-tap channel="logger"/>

<logging-channel-adapter id="logger" log-full-message="true" logger-name="message.history"/>

拦截应用程序中的所有频道并记录消息及其消息历史.

To intercept all the channels in the application and log messages with their message history.

您可以为全局 创建自己的 POJO 订阅者()并执行一些智能逻辑来提取 code>MessageHistory 来自消息:MessageHistory.read(Message).这样的 MessageHistory 是一个 List,实际上你可以在 MessageHistory.Entry 中转换 Properties类并遍历其属性:

You may create your own POJO subscriber (<service-activator>) for the global <wire-tap> and perform some smart logic to extract a MessageHistory from the message: MessageHistory.read(Message<?>). Such a MessageHistory is a List<Properties> where, actually you can cast that Properties in the MessageHistory.Entry class and walk over its properties:

    public String getName() {
        return this.getProperty(NAME_PROPERTY);
    }

    public String getType() {
        return this.getProperty(TYPE_PROPERTY);
    }

    public String getTimestamp() {
        return this.getProperty(TIMESTAMP_PROPERTY);
    }

使用 name,您可以转到 BeanFactory 以获取真正的组件实例,并且已经在那里尝试为您的目的提取所需的属性,但同样:不是所有的它们将可用只是因为...

With the name you can go to the BeanFactory to get the real component instance and already there try to extract required properties for your purpose, but again: not all of them are going to be available just because...

您也可以咨询 IntegrationNode 实现的可能公共属性的nofollow noreferrer">集成图.

You also can consult with the Integration Graph for possible public properties of the IntegrationNode implementations.

这篇关于Spring Integration:如何拦截所有 spring 集成组件并获取要记录的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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