如何在spring集成中并行和同步处理?

m.a*_*sik 5 java ftp spring multithreading spring-integration

在 Spring 集成中是否可以保持通道同步(在发送消息后获得确认)但同时处理更多消息(并行处理)而不用创建自己的线程代码(即 ExecutorService 执行和提交工作线程)并等待它们? 我想通过 FTP 上传文件,但同时上传更多文件,而无需在代码中创建自己的线程。我需要知道何时上传所有文件(这就是为什么我希望它是同步的)。是否可以通过 Spring 集成配置,如果可以,如何实现?

Art*_*lan 3

好吧,看起来你需要一些流程,例如:

  1. <gateway>将文件发送到通道并等待一些结果作为确认

  2. <splitter>并行ExecutorChannel处理每个文件

  3. <int-ftp:outbound-gateway>上传每个文件

  4. <aggregator>关联和分组结果<int-ftp:outbound-gateway>

  5. <aggregator>应该将其结果发送到<gateway>当时正在等待的 。

如果有不清楚的地方,请告诉我。

更新

如何在 Spring Integration Java DSL 中做到这一点?有任何示例吗?

像这样的东西:

@Configuration
@EnableIntegration
@IntegrationComponentScan
public class Configuration {

    @Bean
    public IntegrationFlow uploadFiles() {
        return f ->
                   f.split()
                       .handle(Ftp.outboundGateway(this.ftpSessionFactory,
                           AbstractRemoteFileOutboundGateway.Command.PUT, "'remoteDirectory'"))
                       .aggregate();
    }

}

@MessagingGateway(defaultRequestChannel = "uploadFiles.input") 
interface FtpUploadGateway {

    List<String> upload(List<File> filesToUpload);

}
Run Code Online (Sandbox Code Playgroud)