0
votes

I am very new to Spring Integration and my project is using File Support to read a file and load into data base.

I have XML config , trying to understand it's content.

<int-file:inbound-channel-adapter auto-startup= true channel="channelOne" directory="${xx}" filename-regex="${xx}" id="id" prevent-duplicates="false">
        <int:poller fixed-delay="1000" receive-timeout="5000"/>
</int-file:inbound-channel-adapter>        

<int:channel id="channelOne"/>

From the above piece, my understanding is :

  1. We define a channel and
  2. Then define inbound-channel-adapter - this will look into directory for the file and create a message with file as a payload.

I was able to convert this in JavaConfig as below :

@Bean
public MessageChannel fileInputChannel() {
    return new DirectChannel();
}

@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<File> fileReadingMessageSource() {
        FileReadingMessageSource sourceReader= new FileReadingMessageSource();
        RegexPatternFileListFilter regexPatternFileListFilter = new RegexPatternFileListFilter(
                file-regex);

        //List<FileListFilter<File>> fileListFilter = new ArrayList<FileListFilter<File>>();
        fileListFilter.add(regexPatternFileListFilter);
        //CompositeFileListFilter compositeFileListFilter = new CompositeFileListFilter<File>(
                fileListFilter);
        sourceReader.setDirectory(new File(inputDirectorywhereFileComes));
        sourceReader.setFilter(regexPatternFileListFilter );
        return sourceReader;
    }

Then the next piece of code , which literally I am struggling to understand and moreover to convert to JavaConfig.

Here is the next piece:

<int-file:outbound-gateway
        delete-source-files="true"
        directory="file:${pp}"
        id="id"
        reply-channel="channelTwo"
        request-channel="channelOne"
        temporary-file-suffix=".tmp"/>

<int:channel id="channelTwo"/>
<int:outbound-channel-adapter channel="channelTwo" id="id" method="load" ref="beanClass"/>

So from this piece , my understanding :

1: Define an output channel. 2: Define an outbound-gateway, which will write that message as a file again in directory(other one), also remove file from source directory. And finally it will call the method Load of Bean Class. This is our class and has load method which takes file as input and load it to DB.

I tried to covert it into Java Config. Here is my code:

 @Bean
    @ServiceActivator(inputChannel= "fileInputChannel")
    public MessageHandler fileWritingMessageHandler() throws IOException, ParseException {
        FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(path to output directory));
        handler.setFileExistsMode(FileExistsMode.REPLACE);

    beaObject.load(new File(path to output directory or input directory:: Nothing Worked));
        handler.setDeleteSourceFiles(true);
        handler.setOutputChannel(fileOutputChannel());
        return handler;
    }

I am able to write this file to output folder also was able to delete from source. After that I am totally lost. I have to call method Load of my BeanClass(ref=class in XML ).

I tried a lot, but not able to get it. Read multiple times the integration File Support doc, but couldn't make it.

Note: When I tried , I got one error saying , the File Not Found Exception. I believe , I am able to call my method , but can not get the file.

This XML config is working perfectly fine.

Spring Integration with DSL also anyone can suggest, if possible.

Please help me to understand the basic flow and get this thing done. Any help and comments is really appreciable.

Thanks in advance.

1

1 Answers

0
votes

First of all you need to understand that @Bean method is exactly for configuration and components definitions which are going to be used later at runtime. You definitely must not call a business logic in the @Bean. I mean that your beaObject.load() is totally wrong.

So, please, go first to Spring Framework Docs to understand what is @Bean and its parent @Configuration: https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/core.html#beans-java

Your @ServiceActivator for the FileWritingMessageHandler is really correct (when you remove that beaObject.load()). What you just need is to declare one more @ServiceActivator for calling your beaObject.load() at runtime when message appears in the fileOutputChannel:

@ServiceActivator(inputChannel= "fileOutputChannel")
public void loadFileIntoDb(File payload) {
       this.beaObject.load(payload);
}

See https://docs.spring.io/spring-integration/docs/5.1.1.BUILD-SNAPSHOT/reference/html/configuration.html#annotations for more info.