3
votes

Is it possible to register MessageSources at runtime with spring-integration-dsl?

In my case I want to create multiple FileReadingMessageSources (based on input from UI) and then send the payload to a specific channel/jms route (which is read from metadata or user input)

Another Question is, is it possible to dynamically register IntegrationFlows?

1

1 Answers

3
votes

It's a bit tricky and requires some Spring infrastructure knowledges, but yes it is possible:

@Service
public static class MyService {

    @Autowired
    private AutowireCapableBeanFactory beanFactory;

    @Autowired
    @Qualifier("dynamicAdaptersResult")
    PollableChannel dynamicAdaptersResult;

    public void pollDirectories(File... directories) {
        for (File directory : directories) {
            StandardIntegrationFlow integrationFlow = IntegrationFlows
                    .from(s -> s.file(directory),
                            e -> e.poller(p -> p.fixedDelay(1000))
                                    .id(directory.getName() + ".adapter"))
                    .transform(Transformers.fileToString(),
                            e -> e.id(directory.getName() + ".transformer"))
                    .channel(this.dynamicAdaptersResult)
                    .get();
            this.beanFactory.initializeBean(integrationFlow, directory.getName());
            this.beanFactory.getBean(directory.getName() + ".transformer", Lifecycle.class).start();
            this.beanFactory.getBean(directory.getName() + ".adapter", Lifecycle.class).start();
        }
    }

}

Investigate this my sample, please, and let me know what isn't clear for you.