Search code examples
rssspring-integrationspring-dsl

spring-integration-dsl: Make Feed-Flow Work


I'm trying to code a RSS-feed reader with a configured set of RSS-feeds. I thought that a good approach is to solve that by coding a prototype-@Bean and call it with each RSS-feed found in the configuration.

However, I guess that I'm missing a point here as the application launches, but nothing happens. I mean the beans are created as I'd expect, but there is no logging happening in that handle()-method:

@Component
public class HomeServerRunner implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(HomeServerRunner.class);

    @Autowired
    private Configuration configuration;

    @Autowired
    private FeedConfigurator feedConfigurator;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<IntegrationFlow> feedFlows = configuration.getRssFeeds()
            .entrySet()
            .stream()
            .peek(entry -> System.out.println(entry.getKey()))
            .map(entry -> feedConfigurator.feedFlow(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
        // this one appears in the log-file and looks good
        logger.info("Flows: " + feedFlows);
    }

}

@Configuration
public class FeedConfigurator {

    private static final Logger logger = LoggerFactory.getLogger(FeedConfigurator.class);

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public IntegrationFlow feedFlow(String name, FeedConfiguration configuration) {
        return IntegrationFlows
                .from(Feed
                        .inboundAdapter(configuration.getSource(), getElementName(name, "adapter"))
                        .feedFetcher(new HttpClientFeedFetcher()),
                        spec -> spec.poller(Pollers.fixedRate(configuration.getInterval())))
                .channel(MessageChannels.direct(getElementName(name, "in")))
                .enrichHeaders(spec -> spec.header("feedSource", configuration))
                .channel(getElementName(name, "handle"))
        //
        // it would be nice if the following would show something:
        //
                .handle(m -> logger.debug("Payload: " + m.getPayload()))
                .get();
    }

    private String getElementName(String name, String postfix) {
        name = "feedChannel" + StringUtils.capitalize(name);
        if (!StringUtils.isEmpty(postfix)) {
            name += "." + postfix;
        }
        return name;
    }

}

What's missing here? It seems as if I need to "start" the flows somehow.


Solution

  • Prototype beans need to be "used" somewhere - if you don't have a reference to it anywhere, no instance will be created.

    Further, you can't put an IntegrationFlow @Bean in that scope - it generates a bunch of beans internally which won't be in that scope.

    See the answer to this question and its follow-up for one technique you can use to create multiple adapters with different properties.

    Alternatively, the upcoming 1.2 version of the DSL has a mechanism to register flows dynamically.