此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-doc.cadn.net.cn

TCP 连接拦截器

您可以使用对TcpConnectionInterceptorFactoryChain. 可以使用拦截器向连接添加行为,例如协商、安全性和其他选项。 该框架当前没有提供拦截器,但请参阅InterceptedSharedConnectionTests在源存储库中举个例子。spring-doc.cadn.net.cn

HelloWorldInterceptor用于测试用例的工作原理如下:spring-doc.cadn.net.cn

拦截器首先配置了客户端连接工厂。 当第一条消息通过拦截的连接发送时,拦截器通过连接发送“Hello”,并期望收到“world!”。 发生这种情况时,协商完成并发送原始消息。 此外,使用相同连接的消息无需任何额外协商即可发送。spring-doc.cadn.net.cn

当配置了服务器连接工厂时,拦截器要求第一条消息是“Hello”,如果是,则返回“world!”。 否则,它会抛出导致连接关闭的异常。spring-doc.cadn.net.cn

TcpConnection方法被拦截。 拦截器实例由拦截器工厂为每个连接创建。 如果拦截器是有状态的,则工厂应为每个连接创建一个新实例。 如果没有状态,则同一个拦截器可以包装每个连接。 拦截器工厂被添加到拦截器工厂链的配置中,您可以通过将interceptor-factory属性。 拦截器必须扩展TcpConnectionInterceptorSupport. 工厂必须实现TcpConnectionInterceptorFactory接口。TcpConnectionInterceptorSupport具有直通方法。 通过扩展此类,您只需要实现您希望拦截的方法。spring-doc.cadn.net.cn

以下示例演示如何配置连接拦截器工厂链:spring-doc.cadn.net.cn

<bean id="helloWorldInterceptorFactory"
    class="o.s.i.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
    <property name="interceptors">
        <array>
            <bean class="o.s.i.ip.tcp.connection.HelloWorldInterceptorFactory"/>
        </array>
    </property>
</bean>

<int-ip:tcp-connection-factory id="server"
    type="server"
    port="12345"
    using-nio="true"
    single-use="true"
    interceptor-factory-chain="helloWorldInterceptorFactory"/>

<int-ip:tcp-connection-factory id="client"
    type="client"
    host="localhost"
    port="12345"
    single-use="true"
    so-timeout="100000"
    using-nio="true"
    interceptor-factory-chain="helloWorldInterceptorFactory"/>