Java 如何在camel中将数据从一条路由传递到另一条路由
我是新来的阿帕奇骆驼。我的项目是用camel xml文件编写的 我有一个要求,比如从文件中获取数据,首先通过调用get api检查数据库中的数据是否可用,如果数据存在,则对数据进行一些更改,然后再次调用PUT api来更新数据。若数据不在那个里,那个么只需调用PUT api,它将创建一个新记录 下面是代码,我已经尝试做了一些事情 eqosRoutes.xmlJava 如何在camel中将数据从一条路由传递到另一条路由,java,apache-camel,Java,Apache Camel,我是新来的阿帕奇骆驼。我的项目是用camel xml文件编写的 我有一个要求,比如从文件中获取数据,首先通过调用get api检查数据库中的数据是否可用,如果数据存在,则对数据进行一些更改,然后再次调用PUT api来更新数据。若数据不在那个里,那个么只需调用PUT api,它将创建一个新记录 下面是代码,我已经尝试做了一些事情 eqosRoutes.xml <routeContext id="eqosRoutes" xmlns="http://camel.a
<routeContext id="eqosRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="eqosSupplierFilePollerRoute">
<from
uri="file://{{eqos.feed.polling.directory}}?fileName={{eqos.feed.supplier.file.name}}&doneFileName={{eqos.feed.supplier.trigger.file.name}}&move=.done&moveFailed=.error&delay={{eqos.feed.polling.interval}}&charset={{eqos.feed.character.encoding}}" />
<log
message="EQOS Supplier Feed Poller Route Received a File for processing.."
loggingLevel="INFO" />
<enrich uri="direct:setUserTokenCache" strategyRef="aggregationStrategy" />
<to uri="direct:processEQOSSupplier" />
</route>
<route id="eqosSupplierProcessRoute1">
<from uri="direct:processEQOSSupplier"/>
<log loggingLevel="INFO" message="EQOS Supplier Feed Processing Started" />
<split streaming="true" parallelProcessing="false">
<ref>staxRecordEQOSSupplier</ref>
<log loggingLevel="INFO"
message="EQOS Supplier Feed Processing Started ${body}" />
<choice>
<when>
<simple>${body.supplierId} != null</simple>
<transform>
<method bean="EQOSTransformer" method="transformSupplier" />
</transform>
<doTry>
<to uri="direct:invokegetPartnerAPI" />
<doCatch>
<exception>com.hello.tpil.common.exception.AuthenticationException</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="WARN"
message="invokegetPartnerAPI - AuthenticationException..Retrying for.. ${body}" />
<to uri="direct:setUserTokenCache" />
</doCatch>
<doCatch>
<exception>java.lang.Exception</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="ERROR"
message="invokegetPartnerAPI - Error invoking API - ${body}" />
<log loggingLevel="ERROR"
message="invokegetPartnerAPI - Exception StackTrace - ${exception}" />
</doCatch>
</doTry>
</when>
<otherwise>
<log loggingLevel="INFO"
message="Not a valid body as supplier ID missing..so ignoring for supplier name ${body.partnerName}" />
</otherwise>
</choice>
</split>
<log message="EQOS Supplier Feed Processing Completed."
loggingLevel="INFO" />
</route>
<route id="eqosSupplierProcessRoute">
<from uri="direct:processEQOSSupplier" />
<log loggingLevel="INFO" message="EQOS Supplier Feed Processing Started" />
<split streaming="true" parallelProcessing="false">
<ref>staxRecordEQOSSupplier</ref>
<log loggingLevel="INFO"
message="EQOS Supplier Feed Processing Started ${body}" />
<choice>
<when>
<simple>${body.supplierId} != null</simple>
<transform>
<method bean="EQOSTransformer" method="transformSupplier" />
</transform>
<doTry>
<to uri="direct:invokeCreateOrReplacePartnerAPI" />
<doCatch>
<exception>com.hello.tpil.common.exception.AuthenticationException</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="WARN"
message="invokeCreateOrReplacePartnerAPI - AuthenticationException..Retrying for.. ${body}" />
<to uri="direct:setUserTokenCache" />
</doCatch>
<doCatch>
<exception>java.lang.Exception</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="ERROR"
message="invokeCreateOrReplacePartnerAPI - Error invoking API - ${body}" />
<log loggingLevel="ERROR"
message="invokeCreateOrReplacePartnerAPI - Exception StackTrace - ${exception}" />
</doCatch>
</doTry>
</when>
<otherwise>
<log loggingLevel="INFO"
message="Not a valid body as supplier ID missing..so ignoring for supplier name ${body.partnerName}" />
</otherwise>
</choice>
</split>
<log message="EQOS Supplier Feed Processing Completed."
loggingLevel="INFO" />
</route>
</routeContext>
<routeContext id="tradingPartnerRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="invokeCreateOrReplacePartnerAPI">
<from uri="direct:invokeCreateOrReplacePartnerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<enrich uri="direct:eqosSupplierProcessRoute1" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>PUT</constant>
</setHeader>
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - URL >> {{tps.partner.createorreplace.url}}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Header >> ${headers}" />
<!--<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Body >> ${body}" />-->
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.partner.createorreplace.url}}</simple>
</recipientList>
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Response body >> ${body}" />
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
<route id="invokeCreateOrReplaceCustomerAPI">
<from uri="direct:invokeCreateOrReplaceCustomerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>PUT</constant>
</setHeader>
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - URL >> PUT {{tps.customer.createorreplace.url}}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Header >> ${headers}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Body >> ${body}" />
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.customer.createorreplace.url}}</simple>
</recipientList>
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Response body >> ${body}" />
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
<route id="invokegetPartnerAPI">
<from uri="direct:invokegetPartnerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<!--<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>-->
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setProperty propertyName="oldBody">
<simple>${body}</simple>
</setProperty>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - URL >> {{tps.partner.get.url}}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Header >> ${headers}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Body >> ${body}" />
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.partner.get.url}}?sourceId=52000013&sourceName=EQOS</simple>
</recipientList>
<when>
<simple>${header.CamelHttpResponseCode} == "200"</simple>
<log loggingLevel="DEBUG"
message="Old Data >> ${exchangeProperty[oldBody]}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<to uri="direct:invokeCreateOrReplacePartnerAPI" />
</when>
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
</routeContext>
StaxRecordeQoSupplier
${body.supplierId}!=无效的
com.hello.tpil.common.exception.AuthenticationException
假的
java.lang.Exception
假的
StaxRecordeQoSupplier
${body.supplierId}!=无效的
com.hello.tpil.common.exception.AuthenticationException
假的
java.lang.Exception
假的
TradingPartnerRoutes.xml
<routeContext id="eqosRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="eqosSupplierFilePollerRoute">
<from
uri="file://{{eqos.feed.polling.directory}}?fileName={{eqos.feed.supplier.file.name}}&doneFileName={{eqos.feed.supplier.trigger.file.name}}&move=.done&moveFailed=.error&delay={{eqos.feed.polling.interval}}&charset={{eqos.feed.character.encoding}}" />
<log
message="EQOS Supplier Feed Poller Route Received a File for processing.."
loggingLevel="INFO" />
<enrich uri="direct:setUserTokenCache" strategyRef="aggregationStrategy" />
<to uri="direct:processEQOSSupplier" />
</route>
<route id="eqosSupplierProcessRoute1">
<from uri="direct:processEQOSSupplier"/>
<log loggingLevel="INFO" message="EQOS Supplier Feed Processing Started" />
<split streaming="true" parallelProcessing="false">
<ref>staxRecordEQOSSupplier</ref>
<log loggingLevel="INFO"
message="EQOS Supplier Feed Processing Started ${body}" />
<choice>
<when>
<simple>${body.supplierId} != null</simple>
<transform>
<method bean="EQOSTransformer" method="transformSupplier" />
</transform>
<doTry>
<to uri="direct:invokegetPartnerAPI" />
<doCatch>
<exception>com.hello.tpil.common.exception.AuthenticationException</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="WARN"
message="invokegetPartnerAPI - AuthenticationException..Retrying for.. ${body}" />
<to uri="direct:setUserTokenCache" />
</doCatch>
<doCatch>
<exception>java.lang.Exception</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="ERROR"
message="invokegetPartnerAPI - Error invoking API - ${body}" />
<log loggingLevel="ERROR"
message="invokegetPartnerAPI - Exception StackTrace - ${exception}" />
</doCatch>
</doTry>
</when>
<otherwise>
<log loggingLevel="INFO"
message="Not a valid body as supplier ID missing..so ignoring for supplier name ${body.partnerName}" />
</otherwise>
</choice>
</split>
<log message="EQOS Supplier Feed Processing Completed."
loggingLevel="INFO" />
</route>
<route id="eqosSupplierProcessRoute">
<from uri="direct:processEQOSSupplier" />
<log loggingLevel="INFO" message="EQOS Supplier Feed Processing Started" />
<split streaming="true" parallelProcessing="false">
<ref>staxRecordEQOSSupplier</ref>
<log loggingLevel="INFO"
message="EQOS Supplier Feed Processing Started ${body}" />
<choice>
<when>
<simple>${body.supplierId} != null</simple>
<transform>
<method bean="EQOSTransformer" method="transformSupplier" />
</transform>
<doTry>
<to uri="direct:invokeCreateOrReplacePartnerAPI" />
<doCatch>
<exception>com.hello.tpil.common.exception.AuthenticationException</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="WARN"
message="invokeCreateOrReplacePartnerAPI - AuthenticationException..Retrying for.. ${body}" />
<to uri="direct:setUserTokenCache" />
</doCatch>
<doCatch>
<exception>java.lang.Exception</exception>
<handled>
<constant>false</constant>
</handled>
<log loggingLevel="ERROR"
message="invokeCreateOrReplacePartnerAPI - Error invoking API - ${body}" />
<log loggingLevel="ERROR"
message="invokeCreateOrReplacePartnerAPI - Exception StackTrace - ${exception}" />
</doCatch>
</doTry>
</when>
<otherwise>
<log loggingLevel="INFO"
message="Not a valid body as supplier ID missing..so ignoring for supplier name ${body.partnerName}" />
</otherwise>
</choice>
</split>
<log message="EQOS Supplier Feed Processing Completed."
loggingLevel="INFO" />
</route>
</routeContext>
<routeContext id="tradingPartnerRoutes" xmlns="http://camel.apache.org/schema/spring">
<route id="invokeCreateOrReplacePartnerAPI">
<from uri="direct:invokeCreateOrReplacePartnerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<enrich uri="direct:eqosSupplierProcessRoute1" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>PUT</constant>
</setHeader>
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - URL >> {{tps.partner.createorreplace.url}}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Header >> ${headers}" />
<!--<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Body >> ${body}" />-->
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.partner.createorreplace.url}}</simple>
</recipientList>
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplacePartnerAPI - Response body >> ${body}" />
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
<route id="invokeCreateOrReplaceCustomerAPI">
<from uri="direct:invokeCreateOrReplaceCustomerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>PUT</constant>
</setHeader>
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - URL >> PUT {{tps.customer.createorreplace.url}}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Header >> ${headers}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Body >> ${body}" />
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.customer.createorreplace.url}}</simple>
</recipientList>
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<log loggingLevel="DEBUG"
message="invokeCreateOrReplaceCustomerAPI - Response body >> ${body}" />
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
<route id="invokegetPartnerAPI">
<from uri="direct:invokegetPartnerAPI" />
<doTry>
<enrich uri="direct:getUserTokenCache" strategyRef="aggregationStrategy" />
<setHeader headerName="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<!--<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>-->
<setHeader headerName="X-Client-Id">
<constant>{{identity.client.id}}</constant>
</setHeader>
<setProperty propertyName="oldBody">
<simple>${body}</simple>
</setProperty>
<setExchangePattern pattern="InOut" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - URL >> {{tps.partner.get.url}}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Header >> ${headers}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Body >> ${body}" />
<recipientList parallelProcessing="true">
<simple>cxfrs://{{tps.partner.get.url}}?sourceId=52000013&sourceName=EQOS</simple>
</recipientList>
<when>
<simple>${header.CamelHttpResponseCode} == "200"</simple>
<log loggingLevel="DEBUG"
message="Old Data >> ${exchangeProperty[oldBody]}" />
<log loggingLevel="DEBUG"
message="invokegetPartnerAPI - Response Code >> ${header.CamelHttpResponseCode}" />
<to uri="direct:invokeCreateOrReplacePartnerAPI" />
</when>
<doCatch>
<exception>java.lang.Exception</exception>
<choice>
<when>
<simple>${exception.statusCode} != null and ${exception.statusCode} == "401"</simple>
<log loggingLevel="DEBUG"
message="Authentication Exception at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
<throwException ref="authException" />
</when>
<otherwise>
<log loggingLevel="ERROR"
message="Processing Failed for Record >>> ${exception.statusCode} ${body}" />
<log loggingLevel="ERROR"
message="Error Processing the Line at Index: ${property.CamelSplitIndex}. Exception: ${exception}" />
</otherwise>
</choice>
</doCatch>
</doTry>
</route>
</routeContext>
放
应用程序/json
{{identity.client.id}
cxfrs://{{tps.partner.createorreplace.url}
java.lang.Exception
${exception.statusCode}!=null和${exception.statusCode}==“401”
放
应用程序/json
{{identity.client.id}
cxfrs://{{tps.customer.createorreplace.url}
java.lang.Exception
${exception.statusCode}!=null和${exception.statusCode}==“401”
得到
{{identity.client.id}
${body}
cxfrs://{{tps.partner.get.url}}?sourceId=5200013&;sourceName=EQOS
${header.CamelHttpResponseCode}==“200”
java.lang.Exception
${exception.statusCode}!=null和${exception.statusCode}==“401”
我可以调用getAPI来检查数据是否存在。但在那之后,我必须将数据传递给createOrReplacePartnerAPI,以调用PUT api进行更新
同样,除了检查数据的可用性之外,我不想要GETAPI响应
还有一个问题是,我需要将suplierId动态添加到GET url的URI中,但我无法做到这一点。请在下面找到我的评论: 我可以调用getAPI来检查数据是否存在。但在那之后,我必须将数据传递给createOrReplacePartnerAPI,以调用PUT api进行更新 [Ankit]:调用get api时,exchange主体将替换为响应,文件中的数据将替换。如果在收到get API响应后需要文件(拆分数据),则需要在发送get调用请求之前将正文存储到exchange属性中,并在get API调用之后进行检索。 同样,除了检查数据的可用性之外,我不想要GETAPI响应。 [Ankit]:您可以使用在上述步骤中设置的原始文件数据替换get响应正文。 我面临的另一个问题是,我需要将suplierId动态添加到GET url的URI中,但我无法做到这一点。 [Ankit]:您应该创建cxfrs端点并动态设置http路径,如下所示
<setHeader headerName="CamelHttpPath">
<simple>/address/comune/${header.id}</simple>
</setHeader>
/地址/comune/${header.id}