繁体   English   中英

Spring Integration TCP服务器:获取IP地址和有效负载

[英]Spring Integration TCP server: Get the Ip address & the payload

我刚开始使用Spring-Integration,并实现了TCP服务器,该服务器仅向客户端发送“ OK”消息。 我想记录客户端IP地址和从客户端收到的文本。

我可以使用以下配置文件成功获取客户端发送的文本,但是我不知道如何获取客户端的IP地址。

下面是TCP服务器的配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:int="http://www.springframework.org/schema/integration"
   xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

<context:property-placeholder />
<int-ip:tcp-connection-factory id="tcpServer"
                               type="server"
                               using-nio="true"
                               port="${tcpServer.port}"/>

<int-ip:tcp-inbound-gateway id="tcpGateway"
                            connection-factory="tcpServer"
                            request-channel="bytesChannel"
                            error-channel="errorChannel"/>


<int:service-activator input-channel="inputChannel" ref="myTcpService" method="processInput"/>

<bean id="myTcpService" class="MyTcpService" />

<int:transformer id="transformerBytes2String"
                 input-channel="bytesChannel"
                 output-channel="inputChannel"
                 expression="new String(payload)"/>

<int:transformer id="errorHandler"
                 input-channel="errorChannel"
                 expression="payload.failedMessage.payload + ':' + payload.cause.message"/>

<int:channel id="inputChannel" />
<int:channel id="bytesChannel"/>

</beans>

MyTcpService类:

public class MyTcpService  {
    public String processInput(String input){
       return "OK";
    }
}

我想知道是否有可能在“ processInput”方法中获得IP地址以及有效载荷。

几个连接属性存储在MessageHeadersTcpMessageMapper )中:

messageBuilder
        .setHeader(IpHeaders.HOSTNAME, connection.getHostName())
        .setHeader(IpHeaders.IP_ADDRESS, connection.getHostAddress())
        .setHeader(IpHeaders.REMOTE_PORT, connection.getPort())
        .setHeader(IpHeaders.CONNECTION_ID, connectionId);

因此,您可以简单地在processInput方法中再添加一个参数,以从所需的标头中获取一个值:

public String processInput(String input, @Header(IpHeaders.IP_ADDRESS) String ip){

input参数没有任何注释时,仍映射到payload

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM