骆驼 - 分裂和聚合异常

时间:2014-04-24 07:36:51

标签: apache-camel

我希望以csv文件的形式向web服务端点发送按摩,拆分消息以分别处理每个csv行,聚合检查的异常,并发送带有异常摘要的响应:

我的路线是:

<route>
    <from uri="cxf:bean:MyEndpoint" />
    <split strategyRef="myAggregateStrategy" >
      <tokenize token="\n" />
      <unmarshal>
        <csv delimiter=";" />
      </unmarshal>
      <process ref="MyProcessor" />
      <to uri="bean:myWebservice?method=process" />
    </split>
</route> 

我该怎么做?响应必须发送到webservice

2 个答案:

答案 0 :(得分:1)

如何在逻辑中使用<doTry><doCatch>?你可以拥有你想要的任何逻辑,例如用于处理/聚合/汇总异常的bean。

大致像这样:

<route>
   <from uri="cxf:bean:MyEndpoint" />
   <split strategyRef="myAggregateStrategy" >
      <doTry>
         <tokenize token="\n" />
         <unmarshal>
            <csv delimiter=";" />
         </unmarshal>
         <process ref="MyProcessor" />
         <to uri="bean:myWebservice?method=process" />
         <doCatch>
            <exception>java.lang.Exception</exception>
            <handled>
               <constant>true</constant>
            </handled>
            <bean ref="yourExceptionHandlingBean" method="aggregateException"/>
         </doCatch>
      </doTry>
   </split>
</route> 

答案 1 :(得分:1)

我终于找到了解决问题的方法。我使用了聚合器,如果是异常,请在旧交换机构的列表中收集它并从新交换中删除异常:

public class ExceptionAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        Object body = newExchange.getIn().getBody(String.class);
        Exception exception = newExchange.getException();
        if (exception != null) {
            newExchange.setException(null); // remove the exception
            body = exception;
        }
        if (oldExchange == null) {
            List<Object> list = new ArrayList<>();
            list.add(body);
            newExchange.getIn().setBody(list);
            return newExchange;
        }
        @SuppressWarnings("unchecked")
        List<Object> list = oldExchange.getIn().getBody(List.class);
        list.add(body);
        return oldExchange;
    }
}

列表的类型为java.lang.Object,因为我也会收集原始邮件(如果没有除外)。

相关问题