Camel:Camel和Multicast EIP异常

时间:2013-06-07 16:25:54

标签: routing apache-camel multicast

我有一个简单的例子,我使用3个bean的多播。 thirs bean返回一个RuntimeException,但这很好,因为它不会阻止响应返回给客户端(servlet调用)。但是,如果我在from和多播之间添加dynamicRouter,则不再返回响应,并且我让Camel尝试以不同方式处理异常,方法是将消息重新发送到多播。这会导致转换异常,因为在我的聚合器中我修改了IN消息。

如何保持相同的行为并使用dynamicRouter?

由于

以下是我正在使用的文件的一些摘录:

camel.xml

    <!-- Generic route listening to all the the requests -->
    <route id="generic">
        <from uri="servlet:///services?httpBindingRef=rsaHttpBinding"/>
   <!-- <dynamicRouter>
          <method ref="rsaDynamicRouter" method="route"/>
        </dynamicRouter>
    -->
        <choice> 
            <when> 
                <simple>${property.rsaHeader.action} == 'getAccounts'</simple>
                <to uri="direct:getAccounts#1.0" /> 
            </when> 
            <when>
                <simple>${property.rsaHeader.action} == 'createAccount'</simple>
                <to uri="direct:createAccount#1.0" /> 
            </when> 
        </choice>
    </route>

    <!-- Process getAccounts requests -->
    <route>
        <from uri="direct:getAccounts#1.0" />
        <multicast strategyRef="accountAggregator">
            <to uri="bean:accountBackend1?method=getAccounts" />
            <to uri="bean:accountBackend2?method=getAccounts" />
            <to uri="bean:accountBackend3?method=getAccounts" />
        </multicast>
    </route>

RsaDynamicRouter.java

public class RsaDynamicRouter {
    public String route(Object body, @Properties Map<String, Object> properties) {
    RsaHeader rsaHeader = (RsaHeader)properties.get("rsaHeader");

    return "direct://" + rsaHeader.getAction() + "#" + rsaHeader.getVersion();
    }
}

如果我取消注释dynamicRouter并对选项进行注释,那么我收到的响应不会被发送回客户端而且我有以下异常发生3次:

Caused by: org.apache.camel.InvalidPayloadException: No body available of type: lu.bgl.example.mycamel.account.SearchCriteria but has value: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40] of type: java.util.ArrayList on: Message: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]. Caused by: No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]. Exchange[Message: [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101) ~[camel-core-2.11.0.jar:2.11.0]
    at org.apache.camel.builder.ExpressionBuilder$38.evaluate(ExpressionBuilder.java:934) ~[camel-core-2.11.0.jar:2.11.0]
    ... 131 common frames omitted
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: java.util.ArrayList to the required type: lu.bgl.example.mycamel.account.SearchCriteria with value [Account: number= 1314-1516-1718-1920, name=Larry Page, balance=30, Account: number= 2122-2324-2526-2728, name=Steve Jobs, balance=40]
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:181) ~[camel-core-2.11.0.jar:2.11.0]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99) ~[camel-core-2.11.0.jar:2.11.0]
    ... 132 common frames omitted

1 个答案:

答案 0 :(得分:1)

确定找到了它!

动态路由器必须返回null,否则它会继续重试以执行路由! 我期待只有一个路由而不是多个路由。动态路由器更像是协调员。

无论如何,这是我的动态路由器的代码:

public class RsaDynamicRouter {
    public String route(Object body, @Properties Map<String, Object> properties) {
        RsaHeader rsaHeader = (RsaHeader)properties.get("rsaHeader");

        if (properties.get("endDynamicRouting") == null) {
            properties.put("endDynamicRouting", "dummy");
            return "direct://" + rsaHeader.getAction() + "#" + rsaHeader.getVersion();
        } else {
            return null;
        }
    }
}