Apache camel 驼峰分割和聚合异常
我希望以csv文件的形式向webservice endpoint发送消息,拆分消息以分别处理每个csv行,聚合已检查的异常,并发送带有异常摘要的响应: 我的路线是:Apache camel 驼峰分割和聚合异常,apache-camel,Apache Camel,我希望以csv文件的形式向webservice endpoint发送消息,拆分消息以分别处理每个csv行,聚合已检查的异常,并发送带有异常摘要的响应: 我的路线是: <route> <from uri="cxf:bean:MyEndpoint" /> <split strategyRef="myAggregateStrategy" > <tokenize token="\n" /> <unmarshal
<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在逻辑中使用
和
如何?您可以在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>
java.lang.Exception
真的
我终于找到了解决问题的办法。我使用了聚合器,如果出现异常,则在旧exchange正文的列表中收集该聚合器,并从新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;
}
}
公共类例外聚合策略实现聚合策略{
@凌驾
公共交换聚合(交换旧交换、交换新交换){
objectbody=newExchange.getIn().getBody(String.class);
Exception=newExchange.getException();
if(异常!=null){
newExchange.setException(null);//删除异常
主体=例外;
}
if(oldExchange==null){
列表=新的ArrayList();
增加(正文);
newExchange.getIn().setBody(列表);
返回newExchange;
}
@抑制警告(“未选中”)
List List=oldExchange.getIn().getBody(List.class);
增加(正文);
退换货;
}
}
列表的类型为
java.lang.Object
,因为我也收集原始消息(以防没有例外) 你的解决方案意味着,我必须在静态场上操作。请记住,我需要在路线的和处生成摘要报告。由于响应的格式正确,我需要抛出WebFault ExceptionOni打算使用头来整理异常。很高兴你解决了!