我正在尝试使用routeContext在同一camelContext中重用Camel路由,但我看到使用onException,intercept,dataFormats存在局限性,如如何从其他XML文件导入路由中所述
另一种选择是使用许多 camelContext 和 vm 直接端点进行它们之间的通信,但有一个限制,即只有一个 camelContext 与 Spring Boot 一起使用。在这个替代方案上,我找到了这篇文章如何在 Spring 引导应用程序中配置多个 Camel 上下文。
还有其他选择可以不受任何限制地共享路线吗?
与 Spring Boot 中不接受多个骆驼上下文相关的问题 来了 单一的 configl xml 模型?
添加了更多信息:
我想在一个大路由中构建一个完整的处理工作流,其中包含许多小路由,其中每个路由都有一个特定的任务要执行。我更喜欢XML DSL而不是Java,以便能够使用图形编辑器。
主处理工作流将自动生成(不可修改(,然后开发团队必须仅实现具有特定任务的小路由。一个必要条件是我必须使用Spring Boot。
第一次尝试:一个骆驼上下文并按路由上下文导入路由。使用直接终结点来通信路由。
文件主工作流程.xml
<!-- Import routerContexts-->
<import resource="tranformationIN_route.xml"/>
<import resource="tranformationOUT_route.xml"/>
<camelContext id="mainWorkFlow">
<!-- refer to custom routes -->
<routeContextRef ref="tranformationIN"/>
<routeContextRef ref="tranformationOUT"/>
<route id="main">
<from id="_inRequest" uri="cxf:bean:exposedWS"/>
<to id="_validation" uri="inputData.xsd"/>
<!-- Call route in another context for transformation data received to a backend service data model -->
<to id="_toTransformationInRoute" uri="direct:appTransformationInRoute"/>
<!-- Call backend service -->
<to id="_wsBE" uri="cxf:bean:backendWS"/>
<!-- Call route in another context for transformation data from backend service response to exposed service data model -->
<to id="_toTransformationOutRoute" uri="direct:appTransformationOutRoute"/>
</route>
</camelContext>
文件tranformationIN_route.xml
<routeContext ...>
<endpoint id="reqTrans" uri="dozer:...req_transformation.xml"/>
<!--
Declare dataFormats used by dozer
-->
<dataFormats>
<jaxb contextPath="some.package" id="someID"/>
</dataFormats>
<route id="tranformationIN">
<from uri="direct:appTransformationInRoute"/>
<to id="_to1" uri="ref:reqTrans"/>
</route>
</routeContext>
文件tranformationOUT_route.xml
<routeContext ...>
<endpoint id="reqTrans" uri="dozer:...resp_transformation.xml"/>
<!--
Declare dataFormats used by dozer
-->
<dataFormats>
<jaxb contextPath="some.package" id="someID"/>
</dataFormats>
<route id="tranformationOUT">
<from uri="direct:appTransformationOutRoute"/>
<to id="_to1" uri="ref:respTrans"/>
</route>
</routeContext>
看起来我们不能在路由上下文中使用 dataFormat:
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 6 in XML document from class path resource [spring/custom-routes.xml] is invalid;
nested exception is org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 22; cvc-complex-type.2.4.a: Se ha encontrado contenido no válido a partir del elemento 'dataFormats'.
Se esperaba uno de '{"http://camel.apache.org/schema/spring":route}'...
第二次尝试:许多骆驼语境。使用直接 VM 终结点通信路由。
文件主工作流程.xml
<camelContext id="mainWorkFlow">
<route id="main">
<from id="_inRequest" uri="cxf:bean:exposedWS"/>
<to id="_validation" uri="inputData.xsd"/>
<!-- Call route in another context for transformation data received to a backend service data model -->
<to id="_toTransformationInRoute" uri="direct-vm:appTransformationInRoute"/>
<!-- Call backend service -->
<to id="_wsBE" uri="cxf:bean:backendWS"/>
<!-- Call route in another context for transformation data from backend service response to exposed service data model -->
<to id="_toTransformationOutRoute" uri="direct-vm:appTransformationOutRoute"/>
</route>
</camelContext>
文件appContextTranformationIn_context.xml
<camelContext id="appContextTranformationIn">
<endpoint id="reqTrans" uri="dozer:...req_transformation.xml"/>
<!--
Data forman generated automatically by dozer
If necessary, here I could use dataFormat, onException and interceptor
-->
<dataFormats>
<jaxb contextPath="some.package" id="someID"/>
</dataFormats>
<!-- -->
<route id="tranformationIN">
<from uri="direct-vm:appTransformationInRoute"/>
<to id="_to1" uri="ref:reqTrans"/>
</route>
</camelContext>
文件appContextTranformationOut_context.xml
<camelContext id="appContextTranformationOut">
<endpoint id="reqTrans" uri="dozer:...resp_transformation.xml"/>
<!--
Data forman generated automatically by dozer
If necessary, here I could use dataFormat, onException and interceptor
-->
<dataFormats>
<jaxb contextPath="some.package" id="someID"/>
</dataFormats>
<route id="tranformationOUT">
<from uri="direct-vm:appTransformationOutRoute"/>
<to id="_to1" uri="ref:respTrans"/>
</route>
</camelContext>
问题 Spring Bootk 不喜欢里面跑着不止一头骆驼:/* Routes transformationIN (appContextTranformationIn( 和 transformationOUT (appContextTranformationOut( 将在一个 camelContext 中,但 Spring Boot 的问题是一样的。
您可以从其他路径重用骆驼上下文中的每个路由。这只是您的路线设计方式的问题。
您可以在一条大路线中构建完整的处理工作流。但是,如果您构建第二个工作流,它不能使用第一个路由的任何内容,它根本不可重用。
但是,如果您使用许多小路线构建相同的工作流,其中每个路由都有一个特定的任务要执行,则可以构建第二个工作流,该工作流几乎可以使用第一个路径的每个块。
通常,这些小路由如下所示:
from(direct:validation)
.bean(...)
- 具有一个
direct
端点,可以从每隔一个路由轻松调用它们(同步( - 他们只做一项任务
- 它们没有
to()
"退出",因为它们是请求/回复,因此它们在路由完成后返回给调用方
如果你构建了这样的可重用块,你可以从每个"主"路由调用它们。
from(...)
.to("direct:validation")
.to("direct:transform")
.to("direct:enrich")
...
由于有问题的更多信息而添加
第一次尝试:您不能在routeContext
中使用dataFormat
。您必须将其移动到camelContext
,然后仅从routeContext
引用它。
骆驼文档的片段:
注意:当您使用
routeContext
时,它们是分开的,并且不能重用camelContext
中定义的现有onException
、intercept
、dataFormats
和类似的横切功能。换句话说,routeContext
目前是隔离的。
第二次尝试:如果您使用的是 Spring Boot,为什么要坚持使用 XML 路由?图形编辑器可能很好,但是Java路由并不存在整个问题。Spring Boot 是从 Spring XML config 转向 Java config 的主要驱动力之一。
即使你坚持使用Spring XML配置,但用Java编写路由,你也不会有问题。您可以轻松地将所有RouteBuilders导入到Camel上下文中。
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myMainRoute" />
<routeBuilder ref="mySpecificRoute1" />
<routeBuilder ref="mySpecificRoute2" />
</camelContext>