Spring Integration-自定义errorChannel-仅记录第一个异常 [英] Spring Integration - custom errorChannel - only first exception gets logged

查看:405
本文介绍了Spring Integration-自定义errorChannel-仅记录第一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是上一个问题(原始问题中给出的要求)的后续内容。

This is a follow up to the previous question (requirements given in original question).

Spring集成-过滤器-将消息发送到其他端点

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

My issue is that if there is more than one error in the input file, only the first error is getting logged. The subsequent errors are not getting logged.

修改后的代码:

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

我期望:

错误摘要报告-

B123  ERROR, FREQUENCY
C123  FREQUENCY_DETAIL

在OUTPUT_FAILED_DIRECTORY中-

In OUTPUT_FAILED_DIRECTORY -

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

我要做什么:(缺少C123信息)

错误的摘要报告-

B123  ERROR, FREQUENCY

在OUTPUT_FAILED_DIRECTORY中-

In OUTPUT_FAILED_DIRECTORY -

B123.json -> stacktrace of error


推荐答案

问题从<$弹出c $ c> .split(userBeanSplitter, splitUserBeans)。

我会说这与我们在普通Java中所做的完全相似与 for 循环。
因此,如果循环中的方法引发异常,则您确实会退出循环,并且不再处理下一个项目。

I would say it is fully similar to what we do in plain Java with a for loop. So, if method in the loop throws an exception, you indeed go away from the loop and the next item is not going to be processed any more.

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

To fix your problem you need to add a .channel(c -> c.executor(myExecutor())) to process splitted items in parallel and have a error handling in the separate thread. This way the loop in the splitter is not going to be affected.

这篇关于Spring Integration-自定义errorChannel-仅记录第一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆