2014-08-27 4 views
0

방금 ​​Spring 통합을 사용하기 시작했으며 클라이언트에 "OK"메시지를 보내는 TCP 서버를 구현했습니다. 클라이언트 IP 주소와 클라이언트로부터받은 텍스트를 기록하고 싶습니다.스프링 통합 TCP 서버 : 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 클래스 : "processInput"방법의 페이로드와 함께 IP 주소를 얻을 수 있다면 내가 알고 싶습니다

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

.

답변

1

여러 연결 속성은 MessageHeaders (TcpMessageMapper)에 저장됩니다

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

:

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

은 그러므로 당신이 간단한 원하는 헤더의 값을 얻을 수 있도록 processInput 방법을 하나 더 인수를 추가 할 수 있습니다 어노테이션이없는 input 인수가 payload에 매핑 된 상태로 남아 있습니다.

+0

'transformerBytes2String' 트랜스 포머를''로 대체 할 수도 있습니다. –