Search code examples
javaspring-bootspring-integration

How To Split Based On a Custom Delimiter


I'm working with spring integration and was wondering how I can override the default delimiter for the @Splitter component. I'm specifically using the FileSplitter component. My goal is to take in a comma separated file via a inbound file adapter and split that file using a comma as a delimiter. Correct me if I'm wrong but the @Splitter component splits files based on '\n' by default. Below is the code that I have so far.

@Bean
@InboundChannelAdapter(value = "fileAdapterInCh" , poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource() {
    log.info("Got a message from channel 'httpInputRequestCh' in fileReadingMessageSource");
    
    FileReadingMessageSource source = new FileReadingMessageSource();
    
    source.setAutoCreateDirectory(true);
    source.setDirectory(new File(inputFilePath));
    
    ChainFileListFilter<File> cflf = new ChainFileListFilter<>();
    cflf.addFilter(new LastModifiedFileListFilter(30));
    cflf.addFilter(new AcceptOnceFileListFilter<>());
    cflf.addFilter(new SimplePatternFileListFilter(fileExtention));
    
    
    source.setFilter(cflf);
    
    List<File> files = source.getScanner().listFiles(new File(inputFilePath));
    source.start();
    
    log.info("Listing all files found in '" + inputFilePath + "': " + files );
    return source;
}


@Bean
@Splitter(inputChannel = "fileAdapterInCh" )
public FileSplitter fileSplitter() {
    log.info("file adapter file splitter "  );
    FileSplitter fs = new FileSplitter();
    fs.setOutputChannelName("testing1");
    return fs;
}

Edit:

Should I be using a FileToStringTransformer to transform the contents of the CSV file to a string then use the DefaultMessageSplitter component since that class has a method to set the delimiters?


Solution

  • The point of FileSplitter is to read file line by line in a streaming way. Since you say that your file is a single line with those commas, then there is nothing we can do from the FileSplitter respective. Because that's the way how BufferedReader works.

    You probably may consider to not use a FileSplitter, but rather read the whole file (than line) into memory and then use a DefaultMessageSplitter with that comma as its delimiter option:

    /**
     * Set delimiters to use for tokenizing String values. The default is
     * <code>null</code> indicating that no tokenization should occur. If
     * delimiters are provided, they will be applied to any String payload.
     *
     * @param delimiters The delimiters.
     */
    public void setDelimiters(String delimiters)