Как избежать исключения MessageDeliveryException? - PullRequest
0 голосов
/ 03 мая 2018

Я пытаюсь отправить простое сообщение через tcp, но я даже не могу справиться с этим с помощью весенней интеграции ... Мне очень скучно с этим ...

Поэтому я попытался использовать 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

Мне очень скучно с Spring ...

1 Ответ

0 голосов
/ 03 мая 2018

Итак, вы успешно отправляете сообщение. Сообщение успешно попадает в outputChannel, который вы выбрали в качестве DirectChannel. DirectChannel по определению требует подписчика, которого я не вижу в вашей конфигурации (например, @Transformer или @ServiceActivator или любой другой тип MessageHandler), и исключение говорит вам именно об этом. Итак, если вы просто хотите проверить, что сообщение отправлено, вы можете выбрать другую реализацию канала. Например, вы можете выбрать QueueChannel, который будет буферизировать сообщения до тех пор, пока они не будут опрошены, или PublishSubscribeChannel, который будет отбрасывать сообщения, если подписчиков нет. Или добавьте подписчика.

@ServiceActivator(inputChannel="outputChannel", outputChannel="replyChannel")
public Message echo(Message message) {
    return message;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...