2017-04-05 56 views
0

使用通道適配器通過TCP發送/接收msg時,發生異常。 我正在嘗試使用TcpSendingMessageHandler將消息發送到套接字,並使用TcpReceivingChannelAdapter接收消息。 在這裏,我能夠發送消息,但收到消息時得到異常。請讓我知道,我在配置中缺少什麼。使用通道適配器通過TCP發送/接收msg時出現異常

我的配置類:

public class SampleTCPConnection { 

    //Client Side 
    @Bean 
    public MessageChannel requestChannel() { 
     return new DirectChannel(); 
    } 

    @ConditionalOnMissingBean(name = "errorChannel") 
    @Bean 
    public MessageChannel errorChannel() { 
     return new DirectChannel(); 
    } 


    @Bean 
    public ByteArrayCrLfSerializer connectionSerializeDeserialize(){ 
     return new ByteArrayCrLfSerializer(); 
    } 

    @Bean 
    @ServiceActivator(inputChannel = "requestChannel") 
    public MessageHandler sendToStreamHost() { 
     TcpSendingMessageHandler gateway = new TcpSendingMessageHandler(); 
     gateway.setConnectionFactory(createConectionFactory()); 
     gateway.setClientMode(true); 

     return gateway; 
    } 

    @Bean 
    public AbstractClientConnectionFactory createConectionFactory(){ 
     AbstractClientConnectionFactory cf = new TcpNetClientConnectionFactory("localhost",8002); 
     cf.setSerializer(connectionSerializeDeserialize()); 
     return cf; 
    } 


    @ServiceActivator(inputChannel = "fromTCPChannel", outputChannel = "replyChannel") 
    public Object replyMessage(Message<String> msg) { 
     return msg.getPayload(); 
    } 

    @ServiceActivator(inputChannel = "errorChannel", outputChannel = "replyChannel") 
    public Object errorMessage(Message<String> msg) { 
     return msg; 
    } 


    //server side 

    @Bean 
    public TcpReceivingChannelAdapter tcpInGate(AbstractServerConnectionFactory connectionFactory) { 
     TcpReceivingChannelAdapter inGate = new TcpReceivingChannelAdapter(); 
     inGate.setConnectionFactory(connectionFactory); 
     inGate.setOutputChannel(fromTCPSocket()); 
     return inGate; 
    } 

    @Bean 
    public AbstractServerConnectionFactory createServerConectionFactory(){ 
     AbstractServerConnectionFactory cf = new TcpNetServerConnectionFactory(8002); 
     cf.setSerializer(connectionSerializeDeserialize()); 
     cf.setDeserializer(connectionSerializeDeserialize()); 
     return cf; 
    } 

    @Bean 
    public MessageChannel fromTCPSocket() { 
     return new DirectChannel(); 
    } 

    @Bean 
    public MessageChannel replyChannel() { 
     return new DirectChannel(); 
    } 
} 

我的網關類如下:

@MessagingGateway(errorChannel = "errorChannel") 
public interface ClientGateway { 

    @Gateway(requestChannel = "requestChannel", replyChannel = "fromTCPSocket") 
    Object sendAndRecieve(@Headers Object customHeaders, Object object); 
} 

,我調用這樣的網關:

public void handleRequest() { 
    Map<String,Object> header = new HashMap<String,Object>(); 
    header.put("ip_connectionId", generateCorrelationId()); 
     gateway.sendAndRecieve(header,"Hello world"); 
} 

而且,不同的是我是獲得如下:

  2017-04-06 01:01:59.739 DEBUG 7460 --- [nio-9010-exec-1] o.s.i.ip.tcp.TcpSendingMessageHandler : Got Connection localhost:8002:51561:d5c4ef5a-dffd-4e41-b724-600d30c6428a 
    2017-04-06 01:01:59.751 DEBUG 7460 --- [nio-9010-exec-1] o.s.i.i.tcp.connection.TcpNetConnection : localhost:8002:51561:d5c4ef5a-dffd-4e41-b724-600d30c6428a Message sent GenericMessage [payload=Hello world, headers={replyChannel=org.springframewor[email protected]79299208, errorChannel=org.springframewor[email protected]79299208, id=8cd8ff75-a4d3-d5b0-7290-b97e3f86d57c, ip_connectionId=af1ea8793d0cbde0, timestamp=1491420719710}] 
    2017-04-06 01:01:59.751 DEBUG 7460 --- [nio-9010-exec-1] ssor$ReplyProducingMessageHandlerWrapper : handler 'TPSConfiguration.sendToStreamHost.serviceActivator.handler' produced no reply for request Message: GenericMessage [payload=Hello world, headers={replyChannel=org.springframewor[email protected]79299208, errorChannel=org.springframewor[email protected]79299208, id=8cd8ff75-a4d3-d5b0-7290-b97e3f86d57c, ip_connectionId=af1ea8793d0cbde0, timestamp=1491420719710}] 
    2017-04-06 01:01:59.751 DEBUG 7460 --- [nio-9010-exec-1] o.s.integration.channel.DirectChannel : postSend (sent=true) on channel 'requestChannel', message: GenericMessage [payload=Hello world, headers={replyChannel=org.springframewor[email protected]79299208, errorChannel=org.springframewor[email protected]79299208, id=8cd8ff75-a4d3-d5b0-7290-b97e3f86d57c, ip_connectionId=af1ea8793d0cbde0, timestamp=1491420719710}] 
    2017-04-06 01:01:59.760 DEBUG 7460 --- [pool-2-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : Accepted connection from 127.0.0.1 
    2017-04-06 01:01:59.760 DEBUG 7460 --- [pool-4-thread-1] o.s.i.i.tcp.connection.TcpNetConnection : localhost:8002:51561:d5c4ef5a-dffd-4e41-b724-600d30c6428a Reading... 
    2017-04-06 01:01:59.767 DEBUG 7460 --- [pool-2-thread-1] o.s.i.i.tcp.connection.TcpNetConnection : New connection 127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f 
    2017-04-06 01:01:59.767 DEBUG 7460 --- [pool-2-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : createServerConectionFactory: Added new connection: 127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f 
    2017-04-06 01:01:59.770 DEBUG 7460 --- [pool-2-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : 127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f Reading... 
    2017-04-06 01:01:59.773 DEBUG 7460 --- [pool-4-thread-1] o.s.i.i.t.s.ByteArrayCrLfSerializer  : Available to read:0 
    2017-04-06 01:01:59.774 DEBUG 7460 --- [pool-2-thread-2] o.s.i.i.t.s.ByteArrayCrLfSerializer  : Available to read:13 
    2017-04-06 01:01:59.774 DEBUG 7460 --- [pool-2-thread-2] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'messageBuilderFactory' 
    2017-04-06 01:01:59.775 DEBUG 7460 --- [pool-2-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : Message received GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=17e75c9d-2cfb-1d0d-4535-db95ceefee50, ip_hostname=127.0.0.1, timestamp=1491420719775}] 
    2017-04-06 01:01:59.775 DEBUG 7460 --- [pool-2-thread-2] o.s.integration.channel.DirectChannel : preSend on channel 'xmlToStringchannel', message: GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=17e75c9d-2cfb-1d0d-4535-db95ceefee50, ip_hostname=127.0.0.1, timestamp=1491420719775}] 
    2017-04-06 01:01:59.775 DEBUG 7460 --- [pool-2-thread-2] o.s.i.handler.ServiceActivatingHandler : ServiceActivator for [org.spr[email protected]2e9f7f54] (TPSConfiguration.replyMessage.serviceActivator.handler) received message: GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=17e75c9d-2cfb-1d0d-4535-db95ceefee50, ip_hostname=127.0.0.1, timestamp=1491420719775}] 
    2017-04-06 01:01:59.776 DEBUG 7460 --- [pool-2-thread-2] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'integrationEvaluationContext' 
    2017-04-06 01:01:59.777 DEBUG 7460 --- [pool-2-thread-2] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'integrationConversionService' 
    2017-04-06 01:01:59.781 DEBUG 7460 --- [pool-2-thread-2] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'replyChannel' 
    2017-04-06 01:01:59.781 DEBUG 7460 --- [pool-2-thread-2] o.s.integration.channel.DirectChannel : preSend on channel 'replyChannel', message: GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=9b9eb07f-8829-67c1-e473-644a3d2883b0, ip_hostname=127.0.0.1, timestamp=1491420719781}] 
    2017-04-06 01:01:59.781 DEBUG 7460 --- [pool-2-thread-2] o.s.integration.handler.BridgeHandler : [email protected] received message: GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=9b9eb07f-8829-67c1-e473-644a3d2883b0, ip_hostname=127.0.0.1, timestamp=1491420719781}] 
    2017-04-06 01:01:59.788 ERROR 7460 --- [pool-2-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : Exception sending message: GenericMessage [payload=byte[11], headers={ip_tcp_remotePort=51561, ip_connectionId=127.0.0.1:51561:8002:61d62f47-77e7-473e-955c-e32585780b6f, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=17e75c9d-2cfb-1d0d-4535-db95ceefee50, ip_hostname=127.0.0.1, timestamp=1491420719775}] 

    org.springframework.messaging.MessagingException: Dispatcher failed to deliver Message; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available 
     at org.springframework.integration.dispatcher.AbstractDispatcher.wrapExceptionIfNecessary(AbstractDispatcher.java:133) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:120) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:231) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:154) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:102) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) ~[spring-messaging-4.2.6.RELEASE.jar:4.2.6.RELEASE] 
     at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:105) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter.onMessage(TcpReceivingChannelAdapter.java:87) ~[spring-integration-ip-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.ip.tcp.connection.TcpNetConnection.run(TcpNetConnection.java:182) ~[spring-integration-ip-4.2.5.RELEASE.jar:na] 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_40] 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_40] 
     at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40] 
    Caused by: org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:226) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:154) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:102) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) ~[spring-integration-core-4.2.5.RELEASE.jar:na] 
     ... 28 common frames omitted 

    2017-04-06 01:01:59.788 DEBUG 7460 --- [pool-2-thread-2] o.s.i.i.t.s.ByteArrayCrLfSerializer  : Available to read:0 

回答

0

對於這種情況,您應該考慮使用TcpOutboundGateway

TcpReceivingChannelAdapterTcpSendingMessageHandler使用不同的插座和有傳入消息中沒有replyChannel信息。

另一方面,我看到你的配置一團糟。您在相同的上下文中聲明client和服務器。同時它不會傷害,但不能用於你的@MessagingGateway配置。我在這裏看到的:你發送消息給client,是的,你收到server,但你的問題是你試圖將它發送到@MessagingGateway回覆。困惑...

請閱讀更多Spring集成參考手冊瞭解@MessagingGateway和TCP通道適配器如何工作。

+0

1)由於我想要異步過程,我正在使用通道適配器。 2)您是否建議在入站和出站通道適配器中使用AbstractClientConnectionFactory? 3)我將在消息頭中設置回覆頻道信息。 4)我將使用MessagingGateway僅將消息發送到出站通道適配器。 感謝您的快速回復。請單獨澄清第二點。 –

+0

反正它不會工作。 MessagingGateway頭文件中的TemporaryReplyChannel不通過網絡。你必須斷開幾個應用程序來區分客戶端和服務器。 –

相關問題