2014-04-24 42 views
2

我想在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服务

回答

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> 
+0

你的解决方案意味着,我将不得不对静态字段进行操作。请记住,我需要在路线和路线上制作总结报告。由于响应的正确格式,我需要抛出异常WebFault此类我 –

+0

所想用头整理例外。很高兴你解决了它! –

1

我终于找到了解决我的问题。我用聚合和异常的情况下,其收集在老替换体的名单上,由新的Exchange删除异常:

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型的,因为我收集原始消息太(在存在的情况下不是除外)。