【问题标题】:Spring Integration - custom errorChannel - only first exception gets loggedSpring Integration - 自定义 errorChannel - 仅记录第一个异常
【发布时间】:2019-09-27 00:15:58
【问题描述】:

这是对上一个问题的跟进(原始问题中给出的要求)。

Spring Integration - Filter - Send messages to a different end point

我的问题是,如果输入文件中有多个错误,则只会记录第一个错误。没有记录后续错误。

修改后的代码:

@Configuration
public class CreateUserConfiguration {
    @Bean
    public IntegrationFlow createUser() {
        return IntegrationFlows.from(Files.inboundAdapter(new File(INPUT_DIR)))
                .enrichHeaders(h -> h.header("errorChannel", "exceptionChannel", true))
                .transform(csvToUserBeanTransformer, "convertCsvToUserBean")
                .split(userBeanSplitter, "splitUserBeans")
                .wireTap(flow -> flow.<UserBean>filter(userBean -> !userBean.getStatus().equalsIgnoreCase("SUCCESS")).channel("errorSummaryReportGenerationChannel"))
                .transform(userBeanToJSONTransformer, "convertUserBeanToJSON")
                .handle(Files.outboundAdapter(new File(OUTPUT_SUCCESS_DIRECTORY)))
                .get();
    }

    @Bean
    public IntegrationFlow logErrorSummary() {
        return IntegrationFlows.from("errorSummaryReportGenerationChannel")
                .handle((p,h) -> {
                    return ((UserBean)(p)).getUserID() + "\t" + ((UserBean)(p)).getStatus();
                })
                .transform(Transformers.objectToString())
                .handle(Files.outboundAdapter(new File(OUTPUT_FAILED_REPORT_FILE_NAME)))
                .get();
    }

    @Bean
    public IntegrationFlow logError() {
        return IntegrationFlows.from("exceptionChannel")
                .enrichHeaders(h -> h.headerExpression("errorFileName", "payload.failedMessage.headers.fileName"))
                .wireTap(flow -> flow.handle(msg -> System.out.println("Received on exceptionChannel " + msg.getHeaders().get("errorFileName"))))
                .transform(Transformers.objectToString())
                .handle(Files.outboundAdapter(new File(generateOutputDirectory(OUTPUT_FAILED_DIRECTORY))).autoCreateDirectory(true).fileExistsMode(FileExistsMode.APPEND).fileNameExpression("getHeaders().get(\"errorFileName\")+'.json'"))
                .get();
    }

    @Bean(name = "exceptionChannel")
    MessageChannel exceptionChannel() {
        return MessageChannels.executor(new SimpleAsyncTaskExecutor()).get();
    }

    @Bean(name="errorSummaryReportGenerationChannel")
    MessageChannel errorSummaryReportGenerationChannel() {
        return DirectChannel();
    }
}

我的期望:

In errorSummaryReport -

B123  ERROR, FREQUENCY
C123  FREQUENCY_DETAIL

在 OUTPUT_FAILED_DIRECTORY -

B123.json -> stacktrace of error
C123.json -> stacktrace of error

我看到了什么:(缺少 C123 信息)

In errorSummaryReport -

B123  ERROR, FREQUENCY

在 OUTPUT_FAILED_DIRECTORY -

B123.json -> stacktrace of error

【问题讨论】:

  • @Artem Bilan,你能帮忙吗?

标签: exception error-handling spring-integration customization


【解决方案1】:

问题从.split(userBeanSplitter, "splitUserBeans")弹出。

我会说它与我们在纯 Java 中使用for 循环所做的完全相似。 因此,如果循环中的方法抛出异常,您确实会离开循环,并且不会再处理下一项。

要解决您的问题,您需要添加.channel(c -&gt; c.executor(myExecutor())) 以并行处理拆分的项目并在单独的线程中进行错误处理。这样分流器中的循环就不会受到影响。

【讨论】:

  • 或者,如果您想按顺序处理它们,您可以在拆分器后添加.gateway(...),并为每个拆分使用其错误通道。
猜你喜欢
  • 2018-03-09
  • 1970-01-01
  • 2011-11-30
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
相关资源
最近更新 更多