英文:
Remove handled file in spring integration DSL flow
问题
这是代码部分的中文翻译:
@Configuration
public class ImportConfiguration {
private static final Logger logger = LoggerFactory.getLogger(ImportConfiguration.class);
@Value("${source.dir.path}")
private String sourceDirectoryPath;
@Value("${dest.dir.path}")
private String destDirectoryPath;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public MessageSource<File> sourceDirectory() {
FileReadingMessageSource messageSource = new FileReadingMessageSource();
messageSource.setDirectory(new File(sourceDirectoryPath));
messageSource.setAutoCreateDirectory(true);
return messageSource;
}
@Bean
public JpaUpdatingOutboundEndpointSpec jpaPersistHandler() {
return Jpa.outboundAdapter(this.entityManagerFactory)
.entityClass(Brand.class)
.persistMode(PersistMode.PERSIST);
}
@Bean
public IntegrationFlow fileMoveFlow() {
return IntegrationFlows.from(sourceDirectory(), conf -> conf.poller(Pollers.fixedDelay(2000)))
.filter(msg -> ((File) msg).getName().endsWith(".txt"))
.transform(new FileToStringTransformer())
.split(s -> s.delimiters("\n"))
.<String, Brand>transform(name -> {
Brand brand = new Brand();
brand.setName(name);
return brand;
})
.handle(jpaPersistHandler(), ConsumerEndpointSpec::transactional)
.get();
}
}
希望这对你有帮助。如果你有其他问题或需要进一步的帮助,请随时告诉我。
英文:
I have such simple flow which read the file from folder and convert the content of this file into several entities in the database (spring-integration-jpa). Just found out that I don't see a simple way how to remove the handled file from the source dir.
Here is the code
@Configuration
public class ImportConfiguration {
private static final Logger logger = LoggerFactory.getLogger(ImportConfiguration.class);
@Value("${source.dir.path}")
private String sourceDirectoryPath;
@Value("${dest.dir.path}")
private String destDirectoryPath;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public MessageSource<File> sourceDirectory() {
FileReadingMessageSource messageSource = new FileReadingMessageSource();
messageSource.setDirectory(new File(sourceDirectoryPath));
messageSource.setAutoCreateDirectory(true);
return messageSource;
}
@Bean
public JpaUpdatingOutboundEndpointSpec jpaPersistHandler() {
return Jpa.outboundAdapter(this.entityManagerFactory)
.entityClass(Brand.class)
.persistMode(PersistMode.PERSIST);
}
@Bean
public IntegrationFlow fileMoveFlow() {
return IntegrationFlows.from(sourceDirectory(), conf -> conf.poller(Pollers.fixedDelay(2000)))
.filter(msg -> ((File) msg).getName().endsWith(".txt"))
.transform(new FileToStringTransformer())
.split(s -> s.delimiters("\n"))
.<String, Brand>transform(name -> {
Brand brand = new Brand();
brand.setName(name);
return brand;
})
.handle(jpaPersistHandler(), ConsumerEndpointSpec::transactional)
.get();
}
}
If you use FileWritingMessageHandler
all is very straight forward because there is a method setDeleteSourceFiles
but with JpaUpdatingOutboundEndpointSpec all is not as simple.
答案1
得分: 1
将ExpressionEvaluatingAdvice
添加到.handle()
中。
在示例存储库中有一个示例(https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/retry-and-more) - xml,但使用DSL相同的技术适用。
英文:
Add an ExpressionEvaluatingAdvice
to the .handle()
.
There's an example in the samples repo - xml, but the same techniques apply with the DSL.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论