Phoste
Phoste

Reputation: 1209

How to avoid the MessageDeliveryException?

I'm trying to send a simple message through tcp but I can't even manage that using spring integration... I'm really getting bored with that ...

So I tried using TcpOutboundGateway and TcpInboudGateway in client mode but I get a MessageDeliveryException.

Here is my code:

@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;
    }

}

And the scheduled method to send the message :

@Component
public class SimulatorTask {

    @Autowired
    DirectChannel outputChannel;

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

}

The error I get :

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

I'm really getting bored with Spring...

Upvotes: 0

Views: 1117

Answers (1)

Oleg Zhurakousky
Oleg Zhurakousky

Reputation: 6126

So, what happens is that you are sending message successfully. The message does successfully get to the outputChannel which you chose to be a DirectChannel. DirectChannel by definition requires a subscriber, which I don't see in your configuration (such as @Transformer or @ServiceActivator or any other type of MessageHandler), and exception is telling you exactly that. So, if you just want to validate that the message is sent you may want to chose different implementation of channel. For example, you may choose QueueChannel which will buffer the messages until they are polled from it, or PublishSubscribeChannel which will drop messages if there are no subscribers. Or, add a subscriber.

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

Upvotes: 3

Related Questions