回滚频道中的交易

时间:2018-10-04 15:39:28

标签: spring-integration spring-integration-dsl

我有一个Spring Integration流,该流轮询文件夹中的文件,然后尝试将文件的内容发送到Kafka主题。轮询器配置为使用事务管理器(PseudoTransactionManager()),以便在出现问题时将文件移动到“错误”文件夹。

这是流的样子:

  @Bean
  public 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);
  }


public IntegrationFlow inboundFileIntegration(MessageSource<File> fileReadingMessageSource) {

    c -> c.id("inboundFileAdapter")
            .poller(Pollers.fixedDelay(period)   
            .taskExecutor(taskExecutor) 
            .maxMessagesPerPoll(maxMessagesPerPoll)
              .transactionSynchronizationFactory(transactionSynchronizationFactory()) 
                .transactional(transactionManager())))  
        // END POLLER CONFIGURATION
        .transform(fileToObjectTransformer)         
        .channel(INBOUND_BBS_CHANNEL)
        .get();
}



  @Bean
  public IntegrationFlow toKafka(KafkaTemplate<?, ?> kafkaTemplate) {
    return IntegrationFlows.from(INBOUND_BBS_CHANNEL)
          .handle(Kafka.outboundChannelAdapter(kafkaTemplate)

              .topic(this.properties.getEntitlement().getTopic())
            .sync(true)
            .messageKey(this.properties.getEntitlement().getMessageKey()))
        .get();
  }

在“快乐”的情况下,该流程可以完美地工作。但是,例如,如果我杀死了Kafka群集,我希望将错误传播回去,并让transactionalManager将文件移到“错误”文件夹中(并使用{{1}记录问题}。相反,当发生与Kafka相关的错误时,我得到:

wiretap

然后:

Caused by: org.springframework.kafka.core.KafkaProducerException: Failed to send; nested exception is org.apache.kafka.common.errors.TimeoutException:

放置在初始文件夹中的文件不会移动。 我认为问题与“ kafka通道”是单向的事实有关,因此该异常不会传播回txManager所在的初始流。如何获取txManager来“回滚”文件?

---更新---

完整的堆栈跟踪

Caused by: java.lang.IllegalArgumentException: Message payload must be an Expression instance or an expression String.

0 个答案:

没有答案