|
此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 Spring Integration 7.0.4! |
TCP 连接拦截器
您可以使用对 TcpConnectionInterceptorFactoryChain 的引用来配置连接工厂。
您可以使用拦截器为连接添加行为,例如协商、安全性以及其他选项。
框架当前未提供拦截器,但请参阅源仓库中的 InterceptedSharedConnectionTests 作为示例。
测试用例中使用的HelloWorldInterceptor的工作原理如下:
拦截器首先配置一个客户端连接工厂。 当第一条消息通过被拦截的连接发送时,拦截器会通过该连接发送'Hello'并期望收到'world!'。 发生这种情况后,协商即完成,原始消息将被发送。 此外,使用同一连接发送的后续消息将无需任何额外的协商。
当配置了服务器连接工厂时,拦截器要求第一条消息为'Hello',如果是,则返回'world!'。 否则,它将抛出一个导致连接关闭的异常。
所有 TcpConnection 方法都会被拦截。
拦截器实例由拦截器工厂为每个连接创建。
如果拦截器是有状态的,工厂应为每个连接创建一个新实例。
如果没有状态,同一个拦截器可以包装每个连接。
拦截器工厂被添加到拦截器工厂链的配置中,您可以通过设置 interceptor-factory 属性将其提供给连接工厂。
拦截器必须扩展 TcpConnectionInterceptorSupport。
工厂必须实现 TcpConnectionInterceptorFactory 接口。
TcpConnectionInterceptorSupport 具有透传方法。
通过扩展此类,您只需实现希望拦截的方法即可。
以下示例展示了如何配置连接拦截器工厂链:
<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"/>