如何避免MessageDeliveryException?

时间:2018-05-03 11:54:17

标签: spring tcp spring-integration

我试图通过tcp发送一条简单的消息,但我甚至无法使用Spring集成来管理它......我真的对此感到厌倦......

所以我尝试在客户端模式下使用TcpOutboundGateway和TcpInboudGateway,但是我收到了MessageDeliveryException。

这是我的代码:

@EnableIntegration
@IntegrationComponentScan
@Configuration
public class TcpClientConfiguration {

    @Bean
    public TcpNetClientConnectionFactory clientConnectionFactory() {
        TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory("localhost", 7015);
        return factory;
    }

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

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

//    @Bean
//    public TcpOutboundGateway tcpOutGateway(AbstractClientConnectionFactory clientConnectionFactory) {
//        TcpOutboundGateway outGateway = new TcpOutboundGateway();
//        outGateway.setConnectionFactory(clientConnectionFactory);
//        outGateway.setOutputChannel(outputChannel());
//        return outGateway;
//    }

    @Bean
    public TcpInboundGateway tcpInboundGateway(AbstractClientConnectionFactory clientConnectionFactory) {
        TcpInboundGateway inGateway = new TcpInboundGateway();
        inGateway.setConnectionFactory(clientConnectionFactory);
        inGateway.setClientMode(true);
        inGateway.setRequestChannel(outputChannel());
        inGateway.setReplyChannel(replyChannel());
        return inGateway;
    }

}

发送邮件的预定方法:

@Component
public class SimulatorTask {

    @Autowired
    DirectChannel outputChannel;

    @Scheduled( fixedDelay = 3000 )
    public void sendMsg() {
        outputChannel.send(new GenericMessage<>("Hello world!"));
    }

}

我得到的错误:

2018-05-03 13:42:44.578 ERROR 11144 --- [ask-scheduler-7] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application.outputChannel'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=Hello world!, headers={id=ed173189-b102-6f85-5fe5-d901f4585140, timestamp=1525347764578}], failedMessage=GenericMessage [payload=Hello world!, headers={id=ed173189-b102-6f85-5fe5-d901f4585140, timestamp=1525347764578}]
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:445)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:394)
    at be.thingsplay.fmb920simulator.tcp.SimulatorTask.sendMsg(SimulatorTask.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=Hello world!, headers={id=ed173189-b102-6f85-5fe5-d901f4585140, timestamp=1525347764578}]
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:138)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
    ... 16 more

我真的对春天感到厌倦......

1 个答案:

答案 0 :(得分:2)

那么,会发生什么是您正在成功发送消息。消息成功转到您选择作为DirectChannel的outputChannel。 根据定义,DirectChannel需要一个订阅者,我在您的配置中看不到(例如@Transformer@ServiceActivator或任何其他类型的MessageHandler),并且异常正在告诉您那。 因此,如果您只想验证消息是否已发送,您可能需要选择不同的通道实现。例如,您可以选择QueueChannel来缓冲消息,直到它们被轮询,或者PublishSubscribeChannel如果没有订阅者则会丢弃消息。 或者,添加订阅者。

@ServiceActivator(inputChannel="outputChannel", outputChannel="replyChannel")
public Message echo(Message message) {
    return message;
}