另一个选择是使用多个camelContext和vm-direct端点进行通信,但是Spring Boot只能使用一个camelContext。在这个备选方案中,我找到了这篇文章如何在Spring Boot应用程序中配置多个Camel上下文。
是否有其他选择来共享路由而没有任何限制?
与Spring Boot Came单一configl xml模型中不接受多个camel上下文的问题相关。 添加更多信息: 我希望在一个大的路由中构建完整的处理工作流程,其中包含许多具有特定任务的小路由。我倾向于使用XML DSL而不是Java以便使用图形编辑器。
主要的处理工作流程将自动生成(不可修改),然后开发团队只需实现具有特定任务的小路由。其中一个要求是我必须使用Spring Boot。 第一次尝试:一个Camel上下文,并通过routeContext导入路由。使用直接端点来通信路由。
文件mainWorkFlow.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>
File 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>
File 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>
看起来我们不能在路由上下文中使用数据格式:
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}'...
第二次尝试: 多个CamelContext。使用direct-vm端点来通信路由。
文件 mainWorkFlow.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>
File 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>
File 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 Boot不喜欢在其中运行超过一个camelcontext :/ * 路由转换IN(appContextTranformationIn)和转换OUT(appContextTranformationOut)应该在一个camelContext中,但是Spring Boot的问题是相同的。
.to(...)
直接引用它们或者通过在你的路由中定义的消费者(例如direct
、seda
等)接收它们。你能解释一下为什么需要导入路由吗?你是否使用XML配置的路由或直接通过Java配置的路由(自从几个Camel版本以来推荐使用)? - Roman Vottner