TCP Connection Interceptors

你可以使用对 TcpConnectionInterceptorFactoryChain`的引用来配置连接工厂。你可以使用拦截器向连接添加行为,例如协商、安全和其他选项。框架当前未提供任何拦截器,但有关示例,请参见 `InterceptedSharedConnectionTests in the source repository

You can configure connection factories with a reference to a TcpConnectionInterceptorFactoryChain. You can use interceptors to add behavior to connections, such as negotiation, security, and other options. No interceptors are currently provided by the framework, but see InterceptedSharedConnectionTests in the source repository for an example.

HelloWorldInterceptor 在测试用例中使用,具有以下功能:

The HelloWorldInterceptor used in the test case works as follows:

首先使用客户端连接工厂配置拦截器。当通过拦截连接发送第一条消息时,拦截器通过连接发送“Hello”,并预期收到“world!”。当该情况发生时,表示协商已完成,将发送原始消息。而且,使用同一连接的消息会被发送出去,而无需任何附加协商。

The interceptor is first configured with a client connection factory. When the first message is sent over an intercepted connection, the interceptor sends 'Hello' over the connection and expects to receive 'world!'. When that occurs, the negotiation is complete and the original message is sent. Further, messages that use the same connection are sent without any additional negotiation.

使用服务器连接工厂进行配置时,拦截器要求第一条消息为“Hello”,并且如果是这样,将返回“world!”。否则,它将引发一个异常,导致连接关闭。

When configured with a server connection factory, the interceptor requires the first message to be 'Hello' and, if it is, returns 'world!'. Otherwise, it throws an exception that causes the connection to be closed.

所有 TcpConnection 方法都将被拦截。拦截器工厂将按每个连接创建一个拦截器实例。如果拦截器有状态,那么工厂应该为每个连接创建一个新的实例。如果没有状态,则同一个拦截器可以包装每个连接。拦截器工厂将添加至拦截器工厂链的配置,可以通过设置 interceptor-factory 属性为连接工厂提供该配置。拦截器必须扩展 TcpConnectionInterceptorSupport。工厂必须实现 TcpConnectionInterceptorFactory 接口。TcpConnectionInterceptorSupport 具有直通方法。通过扩展此类,您只需要实现您想要拦截的方法。

All TcpConnection methods are intercepted. Interceptor instances are created for each connection by an interceptor factory. If an interceptor is stateful, the factory should create a new instance for each connection. If there is no state, the same interceptor can wrap each connection. Interceptor factories are added to the configuration of an interceptor factory chain, which you can provide to a connection factory by setting the interceptor-factory attribute. Interceptors must extend TcpConnectionInterceptorSupport. Factories must implement the TcpConnectionInterceptorFactory interface. TcpConnectionInterceptorSupport has passthrough methods. By extending this class, you only need to implement those methods you wish to intercept.

以下示例演示如何配置连接拦截器工厂链:

The following example shows how to configure a connection interceptor factory chain:

<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"/>