Search code examples
spring-integrationspring-integration-dsl

Basic Example using Spring Integration to copy file to another directory raising exception


I am a beginner in Spring Integration. I wrote this code which is in spring boot and it is raising exception "Bean named 'messageSource' is expected to be of type 'org.springframework.context.MessageSource' but was actually of type 'org.springframework.integration.file.FileReadingMessageSource'"

Code:

@Configuration
/*@EnableIntegration annotation designates this class as a Spring Integration configuration.*/
@EnableIntegration
public class SIConfig {

    @Bean
    public MessageChannel channel() {
        return new DirectChannel();
    }
    //bydefault name of method
    @Bean
    public MessageSource messageSource() {
        FileReadingMessageSource ms= new FileReadingMessageSource();
        ms.setDirectory(new File("C:\\Users\\payal\\Pictures"));
        ms.setFilter(new SimplePatternFileListFilter("*.mp4"));
        return ms;
    }

    @Bean
    public MessageHandler handler() {
        FileWritingMessageHandler handler= new FileWritingMessageHandler(new File("C:\\Users\\payal\\Documents\\batch7"));
        handler.setFileExistsMode(FileExistsMode.IGNORE);
        handler.setExpectReply(false);
        return handler;

    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from(messageSource(),  configurer -> configurer.poller(Pollers.fixedDelay(10000)))
                .channel(channel())
                .handle(handler()).get();
    }

}

Since using boot, versions are automatically managed

Uploaded code n GitHub too:

https://github.com/LearningNewTechnology/SpringIntegrationOne

Any help would be really appreciated.


Solution

  • Change the name of the bean to, e.g.

    @Bean
    public MessageSource myMessageSource() {
    

    Spring framework (context) has another type of MessageSource and Spring Boot autoconfiguration creates a bean of that type with name messageSource so your bean is colliding with that.