I'm trying to implement a spring integration file polling where multiple servers independently read from a common directory and process the file. Each of them after processing the file rename it .DONE so the other doesn't pick it. After observing some files being picked by both I have used nioLocker as in the sample below. But looks like randomly this doesn't work. Let me know the necessary steps to make nioLocker reliable in a multi-server environment. For other reasons having a seperate mediator (like zookeepr/mongo etc) is not an option.
Thanks.
@Bean
public TransactionSynchronizationFactory transactionSynchronizationFactory() {
ExpressionParser parser = new SpelExpressionParser();
ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor =
new ExpressionEvaluatingTransactionSynchronizationProcessor();
syncProcessor.setAfterCommitExpression(parser.parseExpression("payload.renameTo(new java.io.File(payload.path+'.DONE'))"));
syncProcessor.setAfterRollbackExpression(parser.parseExpression("payload.renameTo(new java.io.File(payload.path+'.DONE'))"));
return new DefaultTransactionSynchronizationFactory(syncProcessor);
}
@Bean
public IntegrationFlow receiveInputFile(@Value("/opt/tomcat/in/test") File in,
@Value(".txt") String pattern,
@Value("${edi.poll.delay.all.edi}") int delay,
@Value("${edi.messages.per.poll.new.order}") int messagesPerPoll) {
//Logging required config for debugging
LOGGER.debug("EDI File pattern :"+pattern);
LOGGER.debug("EDI Delay Seconds :"+delay);
LOGGER.debug("EDI Messages Per Poll :"+messagesPerPoll);
return IntegrationFlows
.from(s -> s.file(in).patternFilter(ServicesConstant.PATTERN_PREFIX + pattern).scanEachPoll(true).nioLocker(),
e -> e.poller(Pollers.fixedDelay(delay).maxMessagesPerPoll(messagesPerPoll)
.transactionSynchronizationFactory(transactionSynchronizationFactory())
.transactional(new PseudoTransactionManager())))
.handle(m -> {
LOGGER.info("Received test file " + m);
LOGGER.info("File path: " +m.getPayload());
LOGGER.info(""+ m.getHeaders().toString());
})
.get();
}