【问题标题】:Camel - split and aggregate exceptionsCamel - 拆分和聚合异常
【发布时间】:2014-04-24 07:36:50
【问题描述】:

我想以 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> 

我该怎么做?响应必须发送到 web 服务

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    在您的逻辑中使用&lt;doTry&gt;&lt;doCatch&gt; 怎么样?你可以在 catch 中有任何你想要的逻辑,例如一个 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> 
    

    【讨论】:

    • 您的解决方案暗示,我必须对静态字段进行操作。请记住,我需要在路线和路线上生成摘要报告。由于响应格式正确,我需要抛出 WebFault 异常
    • 我想使用标题来整理异常。很高兴你解决了!
    【解决方案2】:

    我终于找到了解决问题的方法。我使用了聚合器,如果出现异常,将其收集到旧交换主体的列表中,并从新交换中删除异常:

    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,因为我也收集原始消息(如果没有异常)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多