1

I'm trying to get some log informations from camel bindy. I had a working setup using an BindyCsvDataFormat bindyProduct with an annotated product-bean and an csv file as source.

Now I changed the CSV file and the annotated bean. The processing seams to get stuck within the bindy processor, but I do not get any Informations/logs. My debugProcessor is not reached at all. If I put it before the unmarshal step, then it logs some stuff and I can debug into it. I wonder why the new files do not fit / match any more and why there are no logs OR exceptions or whatever would be of an help.

        from("file:csv-testdata")
        .unmarshal(bindyProduct)    
        .process(debugProcessor)

Thanks in advance AJ

4

1 回答 1

2

在 apache camel 中记录异常

当我问这个问题时,我刚刚开始骑骆驼。与此同时,我发现了一些很好的方法来实现我正在寻找的东西。因此,如果其他人正在寻找这个问题的答案:

1.第一种方法:

为您的项目启用日志记录,正确配置它并通过将日志记录 URL 添加到您的路由来处理日志记录。

1.1 添加依赖项以记录到您的 pom.xml 例如

<groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

1.2 将 log4j.properties 文件添加到您的 src/main/resources 文件夹(如果您想使用 log4j)。在那里定义日志级别、附加程序等

1.3 将日志记录添加到您的路线

from("...")
.unmarshal(bindyProduct)
.to("log:com.your.package.YourChosenName?level=ERROR&multiline=true&showCaughtException=true")
.to(...);

可以在这里找到所有选项的列表 http://camel.apache.org/log.html 你可以使用

&showAll=true 

记录所有信息,如标题、属性、正文等

请记住,URLs "level=" 选项的日志级别的严重性必须等于或高于您在 log4j.properties 文件中定义的级别

2. 第二种方法

定义一个 DebugProcessor 为您记录异常并将其放在处理器之后的路由中,其行为神秘

2.1 编写一个DebugProcessor处理器必须实现public void process(Exchange exchange) throws Exception {

如果您想知道为什么找不到任何异常

exchange.getException()

尝试使用检索它

Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);

用这个异常做你喜欢的事情(登录到 sysout,...)

2.2 将调试器连接到 RouteBuilder Java 类(或其他地方,Spring,XML,...)中的路由

DebugProcessor debugProcessor1 = new DebugProcessor();

from("...")
.unmarshal(bindyProduct)
.process(debugProcessor1)
.to(...);

3. 第三种方法

使用骆驼 .doTry()/.doCatch() 或(也许更好)使用 onException() 路线

http://camel.apache.org/exception-clause.html/camel.apache.org/try-catch-finally.html

3.1 构建一个与要调试的路由分开的 onException() 路由

以下 onException 路由聚合异常并以一种很好的、​​人类可读的方式(通过速度模板)将它们写下来,每 5 秒或 10 个异常:

onException(Exception.class) // OR a special excepion (io, etc)
        .aggregate(header("CamelFileParent"),
                new ExceptionAggregationStrategy())
                .completionSize(10).completionTimeout(5000)
        .to("velocity:velocity/errors.log.vm")
        .to("file:camel/xml-out?fileName=errors-${file:name.noext}-${date:now:yyyy-MM-dd_HH-mm-ss-SSS}.log");

这只是一个例子,你可以做任何你想做的事情,camel 也可以,例如将它们作为邮件发送,将它们放入数据库等。

您现在不需要任何额外的处理器或在您的路线中记录东西:

from("...")
.unmarshal(bindyProduct)
.to(...);
于 2014-10-28T09:52:51.653 回答