Spring集成队列错误处理

时间:2016-12-04 22:27:52

标签: spring-integration

我有一个Spring Integration DSL流程,它从其他API中提取数据,对其进行转换并将其发送到不同的其他API。

在获取数据之后,它会将一条消息发送到队列通道,该通道执行剩余的处理。当队列正在运行时,原始线程将获取更多数据。

我遇到的问题是,在完成处理所有数据之前,不会处理从队列中抛出的任何错误,但我希望它能够停止处理并立即抛出错误,因为整个过程可以采取很长一段时间,但我希望它停止发现第一个错误。

网关:

@MessagingGateway(errorChannel = "syncErrorChannel")
@Service
public interface CrmGateway {
  @Gateway(requestChannel = "departmentSyncInput", replyChannel = "departmentSyncOutput")
  @Payload("new String()")
  Object syncDepartments();
}

流速:

/**
   * Fetches data from the source api and passes it on to the split channel to process it If the
   * response indicates it has more data to fetch then it is also loaded
   *
   * @return {@link IntegrationFlow}
   */
  @Bean
  IntegrationFlow sync() {
    return IntegrationFlows
      .from("departmentSyncInput")
      .handle(this::fetchDomain)
      .enrichHeaders(s -> s.headerExpressions(h -> h.put("nextLink", "payload.getNext()")))
      .routeToRecipients(r -> r
        .recipient("departmentSplitChannel")
        .recipient(
          "departmentSyncInput",
          p -> p.getPayload() instanceof Wrapper
            && ((Wrapper) p.getPayload()).getNext() != null
        ))
      .get();
  }

  /**
   * Split data from the api into individual models and send them to the target service
   *
   * @return {@link IntegrationFlow}
   */
  @Bean
  IntegrationFlow split() {
    return IntegrationFlows
      .from("departmentSplitChannel")
      .transform(Wrapper.class, Wrapper::getContent)
      .split()
      .channel(c -> c.executor(Executors.newScheduledThreadPool(100)))
      .enrichHeaders(h -> h.header("errorChannel", "syncErrorChannel"))
      .handle((payload, headers) -> log("Syncing", payload, payload))
      .transform(Department.class, transformer)
      // exception happens here
      .handle(DepartmentDTO.class, (payload, headers) -> service.upsertDepartment(payload))
      .handle((payload, headers) -> log("Synced", payload, payload))
      .aggregate()
      .get();
  }

错误处理程序:

@Bean
  IntegrationFlow errorHandler() {
    return IntegrationFlows
      .from("syncErrorChannel")
      .handle(Exception.class, (payload, headers) -> {
        payload.printStackTrace();
        return payload;
      })
      .get();
  }

我也尝试使用IntegrationFlows.from("errorChannel")并获得相同的结果。

我也试过使用Future并且它的行为相同,所以当我调用get()时我会收到错误,但最后仍然会发生错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的流程中没有queue频道定义,但我想您的意思是.channel(c -> c.executor())。如果你在这个问题上分享日志会更好。

我可以说您尝试覆盖errorChannel标头,如果是网关,则为TemporaryReplyChannel

因此,错误会发送到网关的进程,并在split的情况下崩溃。

我建议您尝试使用h.header("errorChannel", "syncErrorChannel", true)来覆盖该标题。