Search code examples
javaspringspring-integrationspring-dsl

Why doesn't integration flow subscribe to the from channel?


To me, this appears to be just about the simplest possible spring integration example. I'm trying to learn from the si4demo. But when I run it, I get this exception:

Exception in thread "main" org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application.inbox'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

Where am I going wrong? Doesn't the defined flow create a subscription to the inbox channel?

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.messaging.MessageChannel;

@Configuration
@ComponentScan
@IntegrationComponentScan
public class App {

    public static void main(String[] args) {

        try (ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args)) {

            final Gateway gateway = ctx.getBean(Gateway.class);
            final String rs = gateway.send("hullo");
            System.out.println(rs);

        }

    }

    private static final String INBOX = "inbox";

    @MessagingGateway(defaultRequestChannel = INBOX)
    public interface Gateway {
        String send(String msg);
    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from(INBOX)
                .transform(p -> "world")
                .get();
    }

    @Bean(name = INBOX)
    public MessageChannel inbox() {
        return new DirectChannel();
    }

}

Solution

  • Looks like you have missed the main player - @EnableIntegraion:

    Starting with version 4.0, the @EnableIntegration annotation has been introduced, to allow the registration of Spring Integration infrastructure beans (see JavaDocs). This annotation is required when only Java & Annotation configuration is used, e.g. with Spring Boot and/or Spring Integration Messaging Annotation support and Spring Integration Java DSL with no XML integration configuration.

    http://docs.spring.io/spring-integration/docs/4.3.0.BUILD-SNAPSHOT/reference/html/overview.html#configuration-enable-integration