Apache Camel:DSL(Groovy或Java)中的嵌套过滤器(过滤器...过滤器...结束...过滤器...结束...末端)

时间:2013-05-15 08:38:35

标签: filter nested apache-camel dsl

我们在使用DSL for Camel Routes时遇到了一些问题。似乎无法将以下XML配置转换为Groovy(或JAVA)DSL

    <route id="myroute" >
        <from uri="fromURI" />
        <filter>
            <xpath>//entity[contains(@destinations, 'databasedestination')]</xpath>
            <filter>
                <xpath>//entity[@objectType = 'objectType']</xpath>
                <camel:setHeader headerName="bodyTemp">
                    <simple>${body}</simple>
                </camel:setHeader>
                <filter>
                    <xpath>//entity[@command = 'add']</xpath>
                    <convertBodyTo type="com.foo.bar.Entity" />
                    <bean ref="handler" method="add" />
                </filter>
                <filter>
                    <xpath>//entity[@command = 'foo']</xpath>
                    <convertBodyTo type="com.foo.bar.Entity" />
                    <bean ref="handler" method="foo" />
                </filter>
                <filter>
                    <xpath>//entity[@command = 'bar']</xpath>
                    <convertBodyTo type="com.foo.bar.Entity" />
                    <bean ref="handler" method="bar" />
                </filter>
                <to uri="toURI" />      
            </filter>
        </filter>
    </route>

Wie试图在基于Groovy的RouteBuilder中进行翻译,如下所示:

from("fromURI")
        .filter().xpath('//entity[contains(@destinations, \'databasedestination\')]')
            .setHeader("bodyTemp", simple('${body}'))
            .filter().xpath('//entity[@objectType = \'objectType\']')

                .filter().xpath('//entity[@command = \'add\']')
                    .convertBodyTo(com.foo.bar.Entity)
                    .bean(handler, "add")   
                .end()
                .filter().xpath('//entity[@command = \'foo\']')
                    .convertBodyTo(com.foo.bar.Entity)
                    .bean(handler, "foo")
                .end()
                .filter().xpath('//entity[@command = \'bar\']')
                    .convertBodyTo(com.foo.bar.Entity)
                    .bean(handler, "bar")
                .end()

                .to("toURI")
            .end()
        .end()  

但这显然不起作用。我们收到javax.xml.xpath.XPathExpressionException消息:Invalid xpath: //entity[@command = 'foo']

同时查看相应的堆栈跟踪。

    ->>  814 | doInEvaluateAs      in org.apache.camel.builder.xml.XPathBuilder
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    697 | evaluateAs          in     ''
    |    139 | matches . . . . . . in     ''
    |     47 | process             in org.apache.camel.processor.FilterProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | processNext . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process             in     ''
    |     91 | process . . . . . . in             org.apache.camel.processor.interceptor.TraceInterceptor
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    330 | processErrorHandler in org.apache.camel.processor.RedeliveryErrorHandler
    |    220 | process             in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    303 | process . . . . . . in org.apache.camel.processor.interceptor.DefaultChannel
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    117 | process . . . . . . in org.apache.camel.processor.Pipeline
    |     80 | process             in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    150 | processAsync . . .  in org.apache.camel.processor.UnitOfWorkProcessor
    |    117 | process             in     ''
    |     48 | processNext . . . . in     org.apache.camel.processor.RouteInflightRepositoryProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     86 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    105 | onMessage . . . . . in    org.apache.camel.component.jms.EndpointMessageListener
    |   1110 | runWorker           in java.util.concurrent.ThreadPoolExecutor
    |    603 | run . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    722 | run                 in java.lang.Thread
    Caused by XPathExpressionException: null
    ->>  200 | evaluate            in     com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    800 | doInEvaluateAs      in org.apache.camel.builder.xml.XPathBuilder
    |    697 | evaluateAs . . . .  in     ''
    |    139 | matches             in     ''
    |     47 | process . . . . . . in org.apache.camel.processor.FilterProcessor
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process             in     ''
    |     73 | process . . . . . . in org.apache.camel.management.InstrumentationProcessor
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     91 | process             in    org.apache.camel.processor.interceptor.TraceInterceptor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |    330 | processErrorHandler in org.apache.camel.processor.RedeliveryErrorHandler
    |    220 | process . . . . . . in     ''
    |     45 | processNext         in org.apache.camel.processor.RouteContextProcessor
    |     90 | process . . . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |    303 | process             in org.apache.camel.processor.interceptor.DefaultChannel
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |    117 | process             in org.apache.camel.processor.Pipeline
    |     80 | process . . . . . . in     ''
    |     45 | processNext         in org.apache.camel.processor.RouteContextProcessor
    |     90 | process . . . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |    150 | processAsync        in org.apache.camel.processor.UnitOfWorkProcessor
    |    117 | process . . . . . . in     ''
    |     48 | processNext         in      org.apache.camel.processor.RouteInflightRepositoryProcessor
    |     90 | process . . . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process             in     ''
    |     73 | process . . . . . . in org.apache.camel.management.InstrumentationProcessor
    |     99 | process             in org.apache.camel.util.AsyncProcessorHelper
    |     86 | process . . . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |    105 | onMessage           in     org.apache.camel.component.jms.EndpointMessageListener
    |   1110 | runWorker . . . . . in java.util.concurrent.ThreadPoolExecutor
    |    603 | run                 in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    722 | run . . . . . . . . in java.lang.Thread
    Caused by TransformerException: Unable to evaluate expression using this context
    ->>  367 | execute             in com.sun.org.apache.xpath.internal.XPath
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    129 | eval                in     com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl
    |    105 | eval . . . . . . .  in     ''
    |    187 | evaluate            in     ''
    |    800 | doInEvaluateAs . .  in org.apache.camel.builder.xml.XPathBuilder
    |    697 | evaluateAs          in     ''
    |    139 | matches . . . . . . in     ''
    |     47 | process             in org.apache.camel.processor.FilterProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | processNext . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process             in     ''
    |     91 | process . . . . . . in     org.apache.camel.processor.interceptor.TraceInterceptor
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    330 | processErrorHandler in org.apache.camel.processor.RedeliveryErrorHandler
    |    220 | process             `enter code here`in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    303 | process . . . . . . in org.apache.camel.processor.interceptor.DefaultChannel
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    117 | process . . . . . . in org.apache.camel.processor.Pipeline
    |     80 | process             in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    150 | processAsync . . .  in org.apache.camel.processor.UnitOfWorkProcessor
    |    117 | process             in     ''
    |     48 | processNext . . . . in     org.apache.camel.processor.RouteInflightRepositoryProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     86 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    105 | onMessage . . . . . in     org.apache.camel.component.jms.EndpointMessageListener
    |   1110 | runWorker           in java.util.concurrent.ThreadPoolExecutor
    |    603 | run . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
    ^    722 | run                 in java.lang.Thread
    Caused by RuntimeException: Unable to evaluate expression using this context
    ->>  266 | setRoot             in com.sun.org.apache.xpath.internal.axes.NodeSequence
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    |    214 | execute             in     com.sun.org.apache.xpath.internal.axes.LocPathIterator
    |    339 | execute . . . . . . in com.sun.org.apache.xpath.internal.XPath
    |    129 | eval                in     com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl
    |    105 | eval . . . . . . .  in     ''
    |    187 | evaluate            in     ''
    |    800 | doInEvaluateAs . .  in org.apache.camel.builder.xml.XPathBuilder
    |    697 | evaluateAs          in     ''
    |    139 | matches . . . . . . in     ''
    |     47 | process             in org.apache.camel.processor.FilterProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | processNext . . . . in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process             in     ''
    |     91 | process . . . . . . in     org.apache.camel.processor.interceptor.TraceInterceptor
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    330 | processErrorHandler in org.apache.camel.processor.RedeliveryErrorHandler
    |    220 | process             in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    303 | process . . . . . . in org.apache.camel.processor.interceptor.DefaultChannel
    |     73 | process             in org.apache.camel.util.AsyncProcessorHelper
    |    117 | process . . . . . . in org.apache.camel.processor.Pipeline
    |     80 | process             in     ''
    |     45 | processNext . . . . in org.apache.camel.processor.RouteContextProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    150 | processAsync . . .  in org.apache.camel.processor.UnitOfWorkProcessor
    |    117 | process             in     ''
    |     48 | processNext . . . . in     org.apache.camel.processor.RouteInflightRepositoryProcessor
    |     90 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |     73 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     99 | processNext         in org.apache.camel.processor.DelegateAsyncProcessor
    |     90 | process . . . . . . in     ''
    |     73 | process             in org.apache.camel.management.InstrumentationProcessor
    |     99 | process . . . . . . in org.apache.camel.util.AsyncProcessorHelper
    |     86 | process             in org.apache.camel.processor.DelegateAsyncProcessor
    |    105 | onMessage . . . . . in     org.apache.camel.component.jms.EndpointMessageListener
    |   1110 | runWorker           in java.util.concurrent.ThreadPoolExecutor
    |    603 | run . . `enter code here`. . . . . . in     java.util.concurrent.ThreadPoolExecutor$Worker
    ^    722 | run                 in java.lang.Thread

感谢您的帮助

马库斯

1 个答案:

答案 0 :(得分:0)

您的代码似乎更适合基于内容的路由器,使用选择EIP。

您是否可以使用选择声明? http://camel.apache.org/content-based-router.html