2

Is there an option/way in FileReadingMessageSource to delete file after running receive() method?

Thanks.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
davitjan
  • 31
  • 4

2 Answers2

0

You can do that with this (xml sample):

<int:poller fixed-rate="500">
    <int:transactional synchronization-factory="syncFactory"/>
</int:poller>

<int:transaction-synchronization-factory id="syncFactory">
    <int:after-commit expression="payload.delete()"/>
</int:transaction-synchronization-factory>

where that <poller> is for <int-file:inbound-channel-adapter>

As a transactionManager you can use PseudoTransactionManager: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/transactions.html

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
0

In your method for the message handler use setDeleteSourceFiles(true), see below:

@Bean
@InboundChannelAdapter(value = FILE_CHANNEL_SOURCE, poller = 
@Poller(fixedDelay = INTERVAL))
public MessageSource<File> sourceFiles() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setAutoCreateDirectory(true);
    source.setDirectory(new File(sourceDir));
    source.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
    return source;
}

@Bean
@ServiceActivator(inputChannel = FILE_CHANNEL_SOURCE)
public MessageHandler processedFiles() {
    FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(outputDir));
    handler.setFileExistsMode(FileExistsMode.FAIL);
    handler.setDeleteSourceFiles(true);
    handler.setExpectReply(false);
    return handler;
}
Fatmajk
  • 1,770
  • 1
  • 13
  • 22