Java 删除spring集成DSL流中的已处理文件
我有这样一个简单的流程,从文件夹中读取文件,并将该文件的内容转换为数据库中的几个实体(SpringIntegrationJPA)。刚刚发现我没有看到一种简单的方法可以从源目录中删除处理过的文件 这是密码Java 删除spring集成DSL流中的已处理文件,java,jpa,spring-integration,spring-integration-dsl,Java,Jpa,Spring Integration,Spring Integration Dsl,我有这样一个简单的流程,从文件夹中读取文件,并将该文件的内容转换为数据库中的几个实体(SpringIntegrationJPA)。刚刚发现我没有看到一种简单的方法可以从源目录中删除处理过的文件 这是密码 @Configuration public class ImportConfiguration { private static final Logger logger = LoggerFactory.getLogger(ImportConfiguration.class);
@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();
}
}
@配置
公共类导入配置{
私有静态最终记录器Logger=LoggerFactory.getLogger(ImportConfiguration.class);
@值(${source.dir.path}”)
私有字符串sourceDirectoryPath;
@值(${dest.dir.path}”)
私有字符串destDirectoryPath;
@自动连线
私人实体管理工厂实体管理工厂;
@豆子
public MessageSource sourceDirectory(){
FileReadingMessageSource messageSource=新建FileReadingMessageSource();
setDirectory(新文件(sourceDirectoryPath));
messageSource.setAutoCreateDirectory(true);
返回消息源;
}
@豆子
公共JpaUpdatingOutboundEndpointSpec jpaPersistHandler(){
返回Jpa.outboundAdapter(this.entityManagerFactory)
.entityClass(品牌级)
.persistMode(persistMode.PERSIST);
}
@豆子
公共集成流文件moveflow(){
返回IntegrationFlows.from(sourceDirectory(),conf->conf.poller(Pollers.fixedDelay(2000)))
.filter(msg->((文件)msg.getName().endsWith(“.txt”))
.transform(新文件ToString Transformer())
.split(s->s.分隔符(“\n”))
.transform(名称->{
品牌=新品牌();
brand.setName(名称);
回归品牌;
})
.handle(jpaPersistHandler(),ConsumerEndpointSpec::transactional)
.get();
}
}
如果使用FileWritingMessageHandler
all是非常简单的,因为有一个方法setDeleteSourceFiles
,但是使用JpaUpdatingOutboundEndpointSpec,all就不那么简单了。向.handle()添加一个
这里有一个例子-xml,但同样的技术适用于DSL。FileReadingMessageSource
为我们提供了一个FileHeaders.ORIGINAL\u文件头。要删除该文件,只需从该头文件中调用delete()
代码>