TCP Connection Events

从3.0版本开始,由`TcpConnectionEvent`实例报告`TcpConnection`实例的更改。TcpConnectionEvent`是`ApplicationEvent`的子类,因此可以通过`ApplicationContext`中定义的任何`ApplicationListener`或@EventListener`方法接收。另请参见an event inbound channel adapter

Beginning with version 3.0, changes to TcpConnection instances are reported by TcpConnectionEvent instances. TcpConnectionEvent is a subclass of ApplicationEvent and can thus be received by any ApplicationListener defined in the ApplicationContext, or an @EventListener method. Also see an event inbound channel adapter.

TcpConnectionEvents 具有以下属性:

TcpConnectionEvents have the following properties:

  • connectionId: The connection identifier, which you can use in a message header to send data to the connection.

  • connectionFactoryName: The bean name of the connection factory to which the connection belongs.

  • throwable: The Throwable (for TcpConnectionExceptionEvent events only).

  • source: The TcpConnection. You can use this, for example, to determine the remote IP Address with getHostAddress() (cast required).

与特定连接相关的可用的 TcpConnectionEvent 包括:

Available TcpConnectionEvent s, related to a specific connection include:

  • TcpConnectionOpenEvent

  • TcpConnectionCloseEvent

  • TcpConnectionExceptionEvent

另外,自 4.0 版本起,TCP Connection Factories中讨论的标准反序列化器如今在解码数据流时如果遇到问题,将发出 TcpDeserializationExceptionEvent`实例。这些事件包括异常、正在建立中的缓冲区,以及在发生异常时缓冲区中的偏移量(如果可用)。应用程序可以使用正常的 `ApplicationListener@EventListener`方法或 `ApplicationEventListeningMessageProducer(参见 Receiving Spring Application Events)来捕获这些事件,从而分析问题。

In addition, since version 4.0, the standard deserializers discussed in TCP Connection Factories now emit TcpDeserializationExceptionEvent instances when they encounter problems while decoding the data stream. These events contain the exception, the buffer that was in the process of being built, and an offset into the buffer (if available) at the point where the exception occurred. Applications can use a normal ApplicationListener, an @EventListener method, or an ApplicationEventListeningMessageProducer (see Receiving Spring Application Events) to capture these events, allowing analysis of the problem.

从版本 4.0.7 和 4.1.3 开始,每当服务器套接字上出现意外异常(例如服务器套接字正在使用时的 BindException)时,都会发布 TcpConnectionServerExceptionEvent 实例。这些事件引用连接工厂与原因。

Starting with versions 4.0.7 and 4.1.3, TcpConnectionServerExceptionEvent instances are published whenever an unexpected exception occurs on a server socket (such as a BindException when the server socket is in use). These events have a reference to the connection factory and the cause.

从版本 4.2 开始,每当端点(入站网关或协同出站通道适配器)接收到因 ip_connectionId 标头无效而无法路由到连接的消息时,都会发布 TcpConnectionFailedCorrelationEvent 实例。当收到迟到回复时(发送者线程已超时),出站网关也会发布此事件。该事件包含连接 ID 以及 cause 属性中的异常,其中包含失败消息。

Starting with version 4.2, TcpConnectionFailedCorrelationEvent instances are published whenever an endpoint (inbound gateway or collaborating outbound channel adapter) receives a message that cannot be routed to a connection because the ip_connectionId header is invalid. Outbound gateways also publish this event when a late reply is received (the sender thread has timed out). The event contains the connection ID as well as an exception in the cause property, which contains the failed message.

从版本 4.3 开始,在启动服务器连接工厂时会发出 TcpConnectionServerListeningEvent。当工厂配置为监听端口 0(这意味着操作系统选择该端口)时,此事件很有用。如果您需要在启动某些其他连接到套接字的进程之前进行等待,它还可以用于替代轮询 isListening()

Starting with version 4.3, a TcpConnectionServerListeningEvent is emitted when a server connection factory is started. This is useful when the factory is configured to listen on port 0, meaning that the operating system chooses the port. It can also be used instead of polling isListening(), if you need to wait before starting some other process that connects to the socket.

为了避免收听线程接受连接时出现延迟,该事件在单独的线程上发布。

To avoid delaying the listening thread from accepting connections, the event is published on a separate thread.

从版本 4.3.2 开始,每当无法创建客户端连接时,都会发出 TcpConnectionFailedEvent。该事件的源是连接工厂,您可以使用它来确定无法建立连接的主机与端口。

Starting with version 4.3.2, a TcpConnectionFailedEvent is emitted whenever a client connection cannot be created. The source of the event is the connection factory, which you can use to determine the host and port to which the connection could not be established.

要使用单个 ApplicationListener(或 @EventListener 方法)来接收所有这些事件(包括 TcpConnectionEvent),请将侦听器配置为接收 IpIntegrationEvent

To use a single ApplicationListener (or @EventListener method) to receive all of these events (including TcpConnectionEvent s), configure the listener to receive IpIntegrationEvent s.