Spring Integration:流程完成时调用服务

时间:2018-03-02 11:43:45

标签: spring-integration

我有一个Spring Itegration(5.0.2)流程,它从HTTP端点读取数据并使用HTTP响应中的(拆分)数据发布Kafka消息。

我想执行一个" 最终"在流程完成之前的行动,某种类型的"尝试,捕获,最后"但我不确定实现这一目标的最佳方法是什么。 这是我的代码:

@Bean
public IntegrationFlow startFlow() {
   return IntegrationFlows.from(() -> new GenericMessage<>(""),
  c -> c.id("flow1")
    .poller(
      Pollers.fixedDelay(period, TimeUnit.MILLISECONDS, initialDelay).taskExecutor(taskExecutor)))

  .handle(Http.outboundGateway("http://....)
     .charset("UTF-8")
    .expectedResponseType(String.class)
    .httpMethod(HttpMethod.GET))
  .transform(new transformer())
  .split()
  .channel(CHANNEL_1)
  .controlBus()
  .get();
}

@Bean
public IntegrationFlow toKafka(KafkaTemplate<?, ?> kafkaTemplate) {
return IntegrationFlows.from(CHANNEL_1)

  .handle(Kafka.outboundChannelAdapter(kafkaTemplate)
    // KAFKA SPECIFIC
  .get();
}

基本上,当所有消息都已发送时(请注意我使用.split),我需要调用Spring bean来更新一些数据。

由于

1 个答案:

答案 0 :(得分:1)

如果您在分割器之后谈论“所有消息时”,那么您需要查看.aggregate()https://docs.spring.io/spring-integration/docs/5.0.3.RELEASE/reference/html/messaging-routing-chapter.html#aggregator

拆分器将特殊sequence details标头填充到每个拆分项中,并且聚合器能够使用收到的消息中的标头默认将它们收集到单个实体。

由于您在发送给Kafka后讨论了该过程,因此您应将CHANNEL_1作为PublishSubscribeChannel并将上述.aggregate()作为此频道的最后一位订阅者。更新某些数据的服务应该已经在此聚合器之后。

相关问题