0

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

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

谢谢

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

骆驼.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
4

1 回答 1

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;
        }
    }
}
于 2013-06-10T10:02:33.490 回答