移除Spring Integration DSL流程中已处理的文件。

huangapple go评论100阅读模式
英文:

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(&quot;${source.dir.path}&quot;)
private String sourceDirectoryPath;
@Value(&quot;${dest.dir.path}&quot;)
private String destDirectoryPath;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public MessageSource&lt;File&gt; 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 -&gt; conf.poller(Pollers.fixedDelay(2000)))
.filter(msg -&gt; ((File) msg).getName().endsWith(&quot;.txt&quot;))
.transform(new FileToStringTransformer())
.split(s -&gt; s.delimiters(&quot;\n&quot;))
.&lt;String, Brand&gt;transform(name -&gt; {
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.

huangapple
  • 本文由 发表于 2020年8月4日 00:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定