2017-10-02 15 views
0

私はSpring Integrationを使ってTCP Serverを開発したいと思っていますが、いくつかの問題があります。 私のサーバは、自分のIMEIで自分自身を識別し、サーバからの肯定応答その位置データを同じTCPサーバーに送信します。TcpInboundGatewayでリクエストに応答するにはどうすればよいですか?

私はIMEIを取得することに成功しましたが、承認はデバイスに届きません...私は正しい方法で送信するかどうかはわかりません。私はドキュメンテーションと他の投稿を読んで、チャンネルアダプタの代わりにTcpInboundGatewayを使用しなければならないことを知りました。同じセッションなのでです。しかし、応答をデバイスに返信するには、TcpOutboundGatewayを使用する必要がありますか?

@EnableIntegration 
@IntegrationComponentScan 
@Configuration 
public class TcpServerConfiguration { 

    private static final Logger logger = LogManager.getLogger(TcpServerConfiguration.class); 

    @Autowired 
    TcpServerProperties properties; 

    @Bean 
    public AbstractServerConnectionFactory serverFactory() { 
     TcpNetServerConnectionFactory tcpServerFactory = new TcpNetServerConnectionFactory(properties.getPort()); 
     tcpServerFactory.setDeserializer(new ByteArrayRawSerializer()); 
     return tcpServerFactory; 
    } 

    @Bean 
    public MessageChannel inputChannel() { 
     DirectChannel channel = new DirectChannel(); 
     return channel; 
    } 

    @Bean 
    public MessageChannel outputChannel() { 
     DirectChannel channel = new DirectChannel(); 
     return channel; 
    } 

    @Bean 
    public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory connectionFactory) { 
     TcpInboundGateway inGateway = new TcpInboundGateway(); 
     inGateway.setConnectionFactory(connectionFactory); 
     inGateway.setRequestChannel(inputChannel()); 
     inGateway.setReplyChannel(outputChannel()); 
     return inGateway; 
    } 

} 

メッセージ・ハンドラ・クラス:

は、ここに私のコードです

ここ
@MessageEndpoint 
public class MessageHandler { 

    private static final Logger logger = LogManager.getLogger(MessageHandler.class); 

    @Transformer(inputChannel = "inputChannel", outputChannel = "outputChannel") 
    public byte[] consume(byte[] bytes) { 
     String message = new String(bytes); 
     String byteString = ""; 
     for (byte b : bytes) { 
      byteString += Byte.toString(b) + ", "; 
     } 
     logger.info("Bytes : " + byteString); 
     logger.info("Message : " + message); 
     return new byte[]{ 01 }; 
    } 

} 

私は常に成功した場合されていない送信されますかどうかをテストするためにACK(01)を送信します。

私はlogging.level.org.springframework.integration=DEBUGを追加し、ここで私が得たものである:

2017-10-02 14:16:44.245 INFO 6340 --- [main] b.thingsplay.Fmb920TcpServerApplication : Started Fmb920TcpServerApplication in 3.885 seconds (JVM running for 5.38) 
2017-10-02 14:16:44.250 INFO 6340 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : serverFactory, port=7015 Listening 
2017-10-02 14:17:50.161 DEBUG 6340 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : Accepted connection from 1.1.1.1 
2017-10-02 14:17:50.200 DEBUG 6340 --- [pool-1-thread-1] o.s.i.i.tcp.connection.TcpNetConnection : New connection ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912 
2017-10-02 14:17:50.201 DEBUG 6340 --- [pool-1-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : serverFactory: Added new connection: ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912 
2017-10-02 14:17:50.204 DEBUG 6340 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912 Reading... 
2017-10-02 14:17:50.207 DEBUG 6340 --- [pool-1-thread-2] o.s.i.i.t.s.ByteArrayRawSerializer  : Available to read:17 
2017-10-02 14:18:20.576 DEBUG 6340 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : Message received GenericMessage [payload=byte[17], headers={ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=229435ad-6284-8136-40d4-4fdfe3ef462e, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700575}] 
2017-10-02 14:18:20.585 INFO 6340 --- [pool-1-thread-2] o.s.i.endpoint.EventDrivenConsumer  : Adding {bridge:null} as a subscriber to the 'outputChannel' channel 
2017-10-02 14:18:20.587 INFO 6340 --- [pool-1-thread-2] o.s.integration.channel.DirectChannel : Channel 'application.outputChannel' has 1 subscriber(s). 
2017-10-02 14:18:20.589 INFO 6340 --- [pool-1-thread-2] o.s.i.endpoint.EventDrivenConsumer  : started [email protected]3d44a 
2017-10-02 14:18:20.595 DEBUG 6340 --- [pool-1-thread-2] o.s.integration.channel.DirectChannel : preSend on channel 'inputChannel', message: GenericMessage [payload=byte[17], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=2cc3d793-dc24-705d-1438-d4b3e089d11b, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700594}] 
2017-10-02 14:18:20.598 DEBUG 6340 --- [pool-1-thread-2] o.s.i.t.MessageTransformingHandler  : messageHandler.consume.transformer.handler received message: GenericMessage [payload=byte[17], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=2cc3d793-dc24-705d-1438-d4b3e089d11b, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700594}] 
2017-10-02 14:18:20.608 INFO 6340 --- [pool-1-thread-2] be.thingsplay.tcp.MessageHandler   : Bytes : 0, 15, 51, 53, 50, 48, 57, 52, 48, 56, 51, 50, 54, 54, 52, 55, 53, 
2017-10-02 14:18:20.611 INFO 6340 --- [pool-1-thread-2] be.thingsplay.tcp.MessageHandler   : Message : 352094083266475 
2017-10-02 14:18:20.618 DEBUG 6340 --- [pool-1-thread-2] o.s.integration.channel.DirectChannel : preSend on channel 'outputChannel', message: GenericMessage [payload=byte[1], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=0e5b6c37-ff73-5c46-4c37-0df48d3ba607, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700614}] 
2017-10-02 14:18:20.620 DEBUG 6340 --- [pool-1-thread-2] o.s.integration.handler.BridgeHandler : [email protected] received message: GenericMessage [payload=byte[1], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=0e5b6c37-ff73-5c46-4c37-0df48d3ba607, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700614}] 
2017-10-02 14:18:20.623 DEBUG 6340 --- [pool-1-thread-2] o.s.integration.channel.DirectChannel : postSend (sent=true) on channel 'outputChannel', message: GenericMessage [payload=byte[1], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=0e5b6c37-ff73-5c46-4c37-0df48d3ba607, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700614}] 
2017-10-02 14:18:20.625 DEBUG 6340 --- [pool-1-thread-2] o.s.integration.channel.DirectChannel : postSend (sent=true) on channel 'inputChannel', message: GenericMessage [payload=byte[17], headers={replyChannel=org.springframewor[email protected]1a0438a2, errorChannel=org.springframewor[email protected]1a0438a2, ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=2cc3d793-dc24-705d-1438-d4b3e089d11b, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700594}] 
2017-10-02 14:18:20.628 DEBUG 6340 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912 Message sent GenericMessage [payload=byte[1], headers={ip_tcp_remotePort=62138, ip_connectionId=ptr-x-x-x-x.dyn.mobistar.be:62138:7015:a7151dbf-63c1-4ff5-b158-5707797b8912, ip_localInetAddress=/0.0.0.0, ip_address=1.1.1.1, id=af87b495-fceb-dbdc-c768-a8a81391af75, ip_hostname=ptr-x-x-x-x.dyn.mobistar.be, timestamp=1506946700628}] 
2017-10-02 14:18:20.631 DEBUG 6340 --- [pool-1-thread-2] o.s.i.i.t.s.ByteArrayRawSerializer  : Available to read:0 

私は、誰かが私を助けることができることを願っています!

答えて

1

正しい方法でTcpInboundGatewayを使用します。 setReplyChannel()オプションは必要ありませんが。ほとんどの場合、ヘッダ内のreplyChannelに依存するだけで十分です。@Transformerメソッドからの返信は、適切な「セッション」に応答して応答します。

私はあなたが間違って使用すると思いますdeserializerByteArrayRawSerializer Javadocを参照してください:あなたのクライアントは、エンド・セッションの接続を閉じていないので

* A byte array (de)serializer that does nothing with the payload; sends it raw. 
* Message termination for assembly purposes is signaled by the client closing the 
* connection. The serializer does not, itself, close the connection after 
* writing the bytes. 
* <p> 
* Because the socket must be closed to indicate message end, this (de)serializer 
* can only be used by uni-directional (non-collaborating) channel adapters, and 
* not by gateways. 

だから、あなたが回答を得ることはありません。このシリアライザはゲートウェイ用ではありません。

はあなたのための適切なターミネータとの適切な(デ)シリアライザーを選択するように考えてみましょう:https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/ip.html#connection-factories

+0

おかげで、それは確かに問題です。しかし、終端値がないときにシリアライザとして使うことはできますか?ドキュメントから私は要求がこのように見えます: 'たとえばIMEI 356307042441013は000f333536333037303432343431303133として送信されます 最初の2バイトはIMEIの長さを示します。この場合、000Fはimeiが15バイト長であることを意味します。 – Phoste

+0

私は分かりません。メッセージの適切なプロトコルに同意するには、クライアントと交渉しなければなりません。 –

関連する問題