Spring Integration事务策略跨越文件入站适配器和队列通道

时间:2019-04-15 11:20:11

标签: spring-integration spring-integration-dsl

我有一个入站文件适配器正在读取的目录,该适配器通过管道进入优先级通道,该优先级通道按文件名对文件进行排序。我已经创建了一个事务同步工厂,用于在处理完成后移动文件,这对于入站适配器以及在其他文件编写器流中发生的所有转换/聚合都适用。一旦添加PriorityChannel,事务似乎就完成了,并且没有传递给转换/聚集逻辑。

这是入站流

return IntegrationFlows
                .from(fileReadingMessageSource,
                        c -> c.poller(Pollers.fixedDelay(period)
                                             .taskExecutor(taskExecutor)
                                             .maxMessagesPerPoll(maxMessagesPerPoll)))
                                             .transactionSynchronizationFactory(transactionSynchronizationFactory())
                                             .transactional(transactionManager())))
                .channel("alphabetically")
                .bridge(s -> s.poller(Pollers.fixedDelay(100)))
                .channel(ApplicationConfiguration.INBOUND_CHANNEL)
                .get();

交易同步策略

@Bean
    TransactionSynchronizationFactory transactionSynchronizationFactory() {
        ExpressionParser parser = new SpelExpressionParser();
        ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
        syncProcessor.setBeanFactory(applicationContext.getAutowireCapableBeanFactory());
        syncProcessor.setAfterCommitExpression(parser.parseExpression(
                "payload.renameTo(new java.io.File(@inboundProcessedDirectory.path " + " + T(java.io.File).separator + payload.name))"));
        syncProcessor.setAfterRollbackExpression(parser.parseExpression(
                "payload.renameTo(new java.io.File(@inboundFailedDirectory.path " + " + T(java.io.File).separator + payload.name))"));
        return new DefaultTransactionSynchronizationFactory(syncProcessor);
    }

有什么想法如何结合优先级队列通道来处理此事务?还是有其他方法可以实现按字母顺序读取文件?

EDIT1

根据加里(Gary)的说法,这应该可以工作(按要求提供整个示例):

@Configuration
class FilePollingIntegrationFlow {

    @Autowired
    public File inboundReadDirectory;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public IntegrationFlow inboundFileIntegration(@Value("${inbound.file.poller.fixed.delay}") long period,
                                                  @Value("${inbound.file.poller.max.messages.per.poll}") int maxMessagesPerPoll, TaskExecutor taskExecutor,
                                                  MessageSource<File> fileReadingMessageSource) {
        return IntegrationFlows
                .from(fileReadingMessageSource,
                        c -> c.poller(Pollers.fixedDelay(period)
                                             .taskExecutor(taskExecutor)
                                             .maxMessagesPerPoll(maxMessagesPerPoll)
                                             .transactionSynchronizationFactory(transactionSynchronizationFactory())
                                             .transactional(transactionManager())))
                .channel(ApplicationConfiguration.INBOUND_CHANNEL)
                .get();
    }

    @Bean
    TaskExecutor taskExecutor(@Value("${inbound.file.poller.thread.pool.size}") int poolSize) {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(poolSize);
        return taskExecutor;
    }

    @Bean
    PseudoTransactionManager transactionManager() {
        return new PseudoTransactionManager();
    }

    @Bean
    TransactionSynchronizationFactory transactionSynchronizationFactory() {
        ExpressionParser parser = new SpelExpressionParser();
        ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
        syncProcessor.setBeanFactory(applicationContext.getAutowireCapableBeanFactory());
        syncProcessor.setAfterCommitExpression(parser.parseExpression(
                "payload.renameTo(new java.io.File(@inboundProcessedDirectory.path " + " + T(java.io.File).separator + payload.name))"));
        syncProcessor.setAfterRollbackExpression(parser.parseExpression(
                "payload.renameTo(new java.io.File(@inboundFailedDirectory.path " + " + T(java.io.File).separator + payload.name))"));
        return new DefaultTransactionSynchronizationFactory(syncProcessor);
    }

    @Bean
    public FileReadingMessageSource fileReadingMessageSource(DirectoryScanner directoryScanner) {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(this.inboundReadDirectory);
        source.setScanner(directoryScanner);
        source.setAutoCreateDirectory(true);
        return source;
    }

    @Bean
    public DirectoryScanner directoryScanner(@Value("${inbound.filename.regex}") String regex) {
        DirectoryScanner scanner = new RecursiveDirectoryScanner();
        CompositeFileListFilter<File> filter = new CompositeFileListFilter<>(
                Arrays.asList(new AcceptOnceFileListFilter<>(), new RegexPatternFileListFilter(regex), new AlphabeticalFileListFilter()));
        scanner.setFilter(filter);
        return scanner;
    }

    private class AlphabeticalFileListFilter implements FileListFilter<File> {
        @Override
        public List<File> filterFiles(File[] files) {
            List<File> list = Arrays.asList(files);
            list.sort(Comparator.comparing(File::getName));
            return list;
        }
    }
}

@Configuration
public class FilePollingConfiguration {

    @Bean(name="inboundReadDirectory")
    public File inboundReadDirectory(@Value("${inbound.read.path}") String path) {
        return makeDirectory(path);
    }

    @Bean(name="inboundProcessedDirectory")
    public File inboundProcessedDirectory(@Value("${inbound.processed.path}") String path) {
        return makeDirectory(path);
    }

    @Bean(name="inboundFailedDirectory")
    public File inboundFailedDirectory(@Value("${inbound.failed.path}") String path) {
        return makeDirectory(path);
    }

    @Bean(name="inboundOutDirectory")
    public File inboundOutDirectory(@Value("${inbound.out.path}") String path) {
        return makeDirectory(path);
    }

    private File makeDirectory(String path) {
        File file = new File(path);
        file.mkdirs();
        return file;
    }

}

通过执行此操作并删除PriorityChannel,似乎事务仍未按我想的那样进行。使用此流,该文件在Http出站网关中不可用。知道为什么吗?

@Component
public class MessageProcessingIntegrationFlow {

    public static final String OUTBOUND_FILENAME_GENERATOR = "outboundFilenameGenerator.handler";
    public static final String FILE_WRITING_MESSAGE_HANDLER = "fileWritingMessageHandler";
    @Autowired
    public File inboundOutDirectory;

    @Bean
    public IntegrationFlow writeToFile(@Value("${api.base.uri}") URI uri,
                                       @Value("${out.filename.dateFormat}") String dateFormat, @Value("${out.filename.suffix}") String filenameSuffix) {
        return IntegrationFlows.from(ApplicationConfiguration.INBOUND_CHANNEL)
                               .enrichHeaders(h -> h.headerFunction(IntegrationMessageHeaderAccessor.CORRELATION_ID, m -> ((String) m
                                       .getHeaders()
                                       .get(FileHeaders.FILENAME)).substring(0, 17)))
                               .aggregate(a -> a.groupTimeout(2000)
                                                .sendPartialResultOnExpiry(true))
                               .transform(m -> {
                                   MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
                                   //noinspection unchecked
                                   ((List<File>) m).forEach(f -> body.add("documents", new FileSystemResource((File) f)));
                                   return body;
                               })
                               .handle(Http.outboundGateway(uri)
                                           .httpMethod(HttpMethod.POST)
                                           .expectedResponseType(byte[].class))
                               .handle(Files.outboundGateway(inboundOutDirectory)
                                            .autoCreateDirectory(true)
                                            .fileNameGenerator(
                                                    m -> m.getHeaders()
                                                          .get(FileHeaders.FILENAME) + "_" + DateTimeFormatter.ofPattern(dateFormat)
                                                                                                              .format(LocalDateTime
                                                                                                                      .now()) + filenameSuffix))
                               .log(LoggingHandler.Level.INFO)
                               .get();
    }
}

2 个答案:

答案 0 :(得分:1)

您不能使用Spring事务切换线程;事务已绑定到线程。

您可以在消息源中使用自定义Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex = 0 Then Me.TblCustomersTableAdapter.Fill(Me.ProjectDatabaseDataSet.tblCustomers) ElseIf ComboBox1.SelectedIndex = 2 Then Me.TblEmployeesTableAdapter.Fill(Me.ProjectDatabaseDataSet.tblEmployees) ElseIf ComboBox1.SelectedIndex = 3 Then Me.TblSalesTableAdapter.Fill(Me.ProjectDatabaseDataSet.tblSales) ElseIf ComboBox1.SelectedIndex = 1 Then Me.TblProductsTableAdapter.Fill(Me.ProjectDatabaseDataSet.tblProducts) End If End Sub 并在那里对文件进行排序。

答案 1 :(得分:0)

感谢加里·罗素,我提出了以下解决方案:

@Bean
    public IntegrationFlow inboundFileIntegration(@Value("${inbound.file.poller.fixed.delay}") long period,
                                                  @Value("${inbound.file.poller.max.messages.per.poll}") int maxMessagesPerPoll,
                                                  @Value("${inbound.file.poller.thread.pool.size}") int poolSize,
                                                  MessageSource<File> fileReadingMessageSource) {
        return IntegrationFlows
                .from(fileReadingMessageSource,
                        c -> c.poller(Pollers.fixedDelay(period)
                                             .taskExecutor(Executors.newFixedThreadPool(poolSize))
                                             .maxMessagesPerPoll(maxMessagesPerPoll)))
                .channel("alphabetically")
                .bridge(s -> s.poller(Pollers.fixedDelay(100)))
                .channel(ApplicationConfiguration.INBOUND_CHANNEL)
                .get();
    }

有关规格的建议:

@Bean
    public Advice fileMoveAdvice() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpression(new FunctionExpression<Message<?>>(m -> renameMultiValueMapFiles(m, this.inboundProcessedDirectory)));
        advice.setOnFailureExpression(new FunctionExpression<Message<?>>(m -> renameMultiValueMapFiles(m, this.inboundFailedDirectory)));
        return advice;
    }

    @Bean
    public Consumer<GenericEndpointSpec<HttpRequestExecutingMessageHandler>> outboundSpec() {
        return new Consumer<GenericEndpointSpec<HttpRequestExecutingMessageHandler>>() {
            @Override
            public void accept(GenericEndpointSpec<HttpRequestExecutingMessageHandler> spec) {
                spec.advice(fileMoveAdvice(), retryAdvice());
            }
        };
    }

@SneakyThrows(IOException.class)
    private boolean renameMultiValueMapFiles(Message<?> m, File directory) {
        MultiValueMap<String, Resource> files = (MultiValueMap<String, Resource>) m.getPayload();
        List<File> list = new ArrayList<>();
        // no lambda to avoid ThrowsFunction type
        for (List<Resource> l : files.values()) {
            for (Resource v : l) {
                list.add(v.getFile());
            }
        }
        list.forEach(v -> v.renameTo(new File(directory.getPath(), v.getName())));
        return true;
    }

添加了要处理的规范:

.handle(Http.outboundGateway(uri)
                            .httpMethod(HttpMethod.POST)
                            .expectedResponseType(byte[].class), this.advices.outboundSpec())