使用通道适配器通过TCP发送/接收消息时发生异常

时间:2017-04-05 19:21:41

标签: spring-integration

我在使用通道适配器通过TCP发送/接收消息时遇到异常。 我正在尝试使用TcpSendingMessageHandler向套接字发送消息,并使用TcpReceivingChannelAdapter接收消息。 在这里,我能够发送消息,但在接收消息时获得异常。请告诉我,配置中缺少的是什么。

我的配置类:

    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.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@79299208, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@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.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@79299208, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@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.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@79299208, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@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.springframework.integration.handler.MethodInvokingMessageProcessor@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    : org.springframework.integration.handler.BridgeHandler@586e121d 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

1 个答案:

答案 0 :(得分:0)

您应该考虑在这种情况下使用TcpOutboundGateway

TcpReceivingChannelAdapterTcpSendingMessageHandler使用不同的套接字,并且传入消息中没有replyChannel个信息。

另一方面,我看到你的配置一团糟。您在相同的上下文中声明client和服务器。同时它没有伤害,但不能应用于您的@MessagingGateway配置。我在此处看到的内容:您向client发送消息,是的,您是通过server收到的,但是您尝试将其发送到@MessagingGateway回复中的问题。困惑...

请阅读更多Spring Integration Reference Manual,了解@MessagingGateway和TCP通道适配器的工作原理。