I have a database with two tables, accountsA and accountsB. I want to do some updates on both of these database tables in the same transaction scope so I used component, but when an exception is happening in updating accountsB, the updates of accountsA is being continued, I need my database to do both updates together or none of them.
for testing that the transacted component is working correctly, I made a change in the name of the accountB table name, an exception accrued. I was expecting that the updating of accountA table will be stop, but it doesn't happened. did I do something wrong?
<bean id="mysql-ds-local" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/BankDB?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="osslab"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysql-ds-local"/>
</bean>
<camelContext id="camel-jdbc-test" xmlns="http://camel.apache.org/schema/blueprint" >
<route id="main-route-jdbc">
<from uri="timer://webinar?period=20000" />
<transacted />
<to uri="direct:reduceCredit"/>
<to uri="direct:increaseCredit"/>
</route>
<route id="reduceCredit-route">
<from uri="direct:reduceCredit"/>
<log message="in direct accountA"/>
<setBody>
<constant>update accountsA set credit = credit + 1 where id = 1</constant>
</setBody>
<to uri="jdbc:mysql-ds-local" />
</route>
<route id="increaseCredit-route">
<from uri="direct:increaseCredit"/>
<log message="in direct accountB"/>
<setBody>
<constant>update accountsB set credit = credit + 1 where id = 1</constant>
</setBody>
<to uri="jdbc:mysql-ds-local" />
</route>
</camelContext>
Perhaps you just omitted it, but a key element that is missing in your question is a DataSourceTransactionManager that makes your DataSource transactional.
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
</bean>
Related
I have a route from which I am trying to call a seda queue. I wanted to use a property which has been set in my current route to the seda queue. The below is the piece of code that I have been used, but it doesn't seems to be working as expected.
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="route1">
<!-- Method initialize the JobDetails class with values jobName="jobTest" and jobMode="mode1" -->
<bean ref="jobDetails" method="createJobDetails"/>
<to uri="activemq:queue:myqueue?disableReplyTo=true" />
</route>
<route id="route2">
<from uri="activemq:queue:myqueue" />
<convertBodyTo type="my.package.JobDetails" />
<setProperty propertyName="JOB_DETAILS">
<simple>${body}</simple>
</setProperty>
<recipientList>
<simple>direct-vm:testDirect-${property.JOB_DETAILS.jobName}</simple>
</recipientList>
</route>
</camelContext>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="route3">
<from uri="direct-vm:testDirect-jobTest" />
<!-- Some business logic based on the job mode-->
<setProperty propertyName="JOB_MODE">
<simple>mode1</simple>
</setProperty>
<to uri="seda:testSeda?blockWhenFull=true" />
</route>
<route id="sedaRoute">
<from uri="seda:testSeda?concurrentConsumers=5" />
<log message="Message from firstRoute: ${property.JOB_MODE}" />
<!-- Perform some business logic based on the job mode -->
</route>
</camelContext>
Appreciated help!!!
I have following camel routing configuration.
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<!-- Rest Configuration -->
<restConfiguration component="jetty" port="9092" bindingMode="auto">
<dataFormatProperty key="json.in.disableFeatures" value="FAIL_ON_UNKNOWN_PROPERTIES" />
</restConfiguration>
<rest path="/" consumes="application/json"
produces="application/json">
<post uri="/" type="com.aaa.xxxx.esb.config.xxxxEsbJsonMapping">
<route>
<setHeader headerName="Authorization">
<simple>Basic YWRtaXXXXWRtaW4=</simple>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<setHeader headerName="RestEndpointURL">
<simple>
http://${body.serviceURL}?bridgeEndpoint=true
</simple>
</setHeader>
<setBody>
<simple>{"UserDetails": ${body.serviceDataJsonObj}}</simple>
</setBody>
<log message="Exchanged headers : ${headers.RestEndpointURL}" />
<recipientList>
<simple>${headers.RestEndpointURL}</simple>
</recipientList>
</route>
</post>
</rest>
What I need to know is where I can set
matchOnUriPrefix=true
option for the jetty component which I have already configured for camel rest.
According to the Claus Ibsen answer, I changed configuration XML as follows.
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<!-- Rest Configuration -->
<restConfiguration component="jetty" port="9092"
bindingMode="auto">
<dataFormatProperty key="json.in.disableFeatures"
value="FAIL_ON_UNKNOWN_PROPERTIES" />
<componentProperty key="matchOnUriPrefix" value="true" />
</restConfiguration>
<!-- Rest Services -->
<rest path="/" consumes="application/json" produces="application/json">
<post uri="/" type="com.aaa.xxxx.esb.config.xxxxEsbJsonMapping">
<route>
<setHeader headerName="Authorization">
<simple>Basic YWRXXX46YWRtaW4=</simple>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<setHeader headerName="RestEndpointURL">
<simple>
http://${body.serviceURL}?bridgeEndpoint=true
</simple>
</setHeader>
<setBody>
<simple>{"SystemUserDetails": ${body.serviceDataJsonObj}}</simple>
</setBody>
<log message="Exchanged headers : ${headers.RestEndpointURL}" />
<recipientList>
<simple>${headers.RestEndpointURL}</simple>
</recipientList>
</route>
</post>
</rest>
</camelContext>
I'm using servicemix apache-servicemix-7.0.0.M2
I upgraded its camel ver 2.16.3 to 2.17.3
Thanks
The rest-dsl is configured using the componentProperty in the restConfiguration:
<restConfiguration component="jetty" port="9092" bindingMode="auto">
<componentProperty key="matchOnUriPrefix" value="true"/>
<dataFormatProperty key="json.in.disableFeatures" value="FAIL_ON_UNKNOWN_PROPERTIES" />
</restConfiguration>
You can find details about this in the documentation: http://camel.apache.org/rest-dsl
U may add inside
route
tag as below:
<route>
<from uri="jetty://foo?matchOnUriPrefix=true"/>
...
</route>
How transactions works in Camel routes with Spring DSL and one of the route throws Exception?
If routeB throws an exception, how exception will be propagated back to routeA.
If exception is thrown in routeB, I could see transactionalErrorHandler is handling the exception and rolling back the transaction.
Setting the errorHandlerRef="noErrorHandler" on routeB does not help.
How can we do this?
My Camel routes definition:
<route id="routeA">
<from uri="direct-vm:endpointA" />
<transacted />
<to uri="direct-vm:endPointB" />
<bean ref="beanA" method="save" />
<onException>
<exception>java.lang.Exception>
<handled><constant>true</constant></handled>
<bean ref="beanA" method="handleError" />
<rollback markRollbackOnly="true" />
</onException>
</route>
<route id="routeB">
<from uri="direct-vm:endPointB" />
<throwException ref="ex"/>
</route>
In camel the transacted command is simply a wrapper for the Spring TransactionTemplate, in that it will enlist the route in a transaction supplied by the PlatformTransactionManager. The only configuration option you get on the org.apache.camel.spring.spi.SpringTransactionPolicy is the propagation behaviour, so handling rollbacks etc must be done within the transacted route.
In this case you should catch the exception from route B, if it gets to the error handler then it is too late.
<route id="routeA">
<from uri="direct-vm:endpointA" />
<transacted/>
<doTry>
<to uri="direct-vm:endPointB" />
<doCatch>
<exception>java.lang.Exception</exception>
<bean ref="beanA" method="handleFail"/>
</doCatch>
<doFinally>
<bean ref="beanA" method="save" />
</doFinally>
</doTry>
</route>
<route id="routeB">
<from uri="direct-vm:endPointB" />
<throwException ref="ex"/>
</route>
I'm using Apache Camel 2.11.1. It seems that both xml definitions are not equivalent in Camel:
First definition:
<bean id="amq" class="org.apache.activemq.camel.component.ActiveMQComponent"
p:brokerURL="tcp://localhost:61616" p:transacted="true"
p:cacheLevelName="CACHE_CONSUMER" p:concurrentConsumers="20"
p:maxConcurrentConsumers="500" p:idleConsumerLimit="10" />
Second definition:
<bean id="amq" class="org.apache.camel.component.jms.JmsComponent"
p:configuration-ref="jmsConfig" p:transacted="true" p:cacheLevelName="CACHE_CONSUMER" />
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"
p:connectionFactory-ref="nakedConnectionFactory"
p:concurrentConsumers="20" p:maxConcurrentConsumers="500"
p:idleConsumerLimit="10" />
<bean id="nakedConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="tcp://localhost:61616" />
Since the first definition works well on the following route, but the second doesn't.
<route>
<from uri="amq:example.MyQueue" />
<setHeader headerName="myRoutingSlipHeader">
<constant>amq:one#amq:two#amq:three#amq:four</constant>
</setHeader>
<log message="Makan" />
<setExchangePattern pattern="InOut" />
<routingSlip uriDelimiter="#">
<header>myRoutingSlipHeader</header>
</routingSlip>
<setExchangePattern pattern="InOnly" />
<log message="End: ${body}" />
</route>
<route>
<from uri="amq:one" />
<to uri="bean:helloBean?method=stepOne" />
</route>
<route>
<from uri="amq:two" />
<to uri="bean:helloBean?method=stepTwo" />
</route>
<route>
<from uri="amq:three" />
<to uri="bean:helloBean?method=stepThree" />
</route>
<route>
<from uri="amq:four" />
<to uri="bean:helloBean?method=stepFour" />
</route>
The second component definition causes hang during execution.
Seems to work fine for me. I did have to override the spring version with the created archetype. But I used this and it worked fine.
Please find my example project here :
https://github.com/jimternet/camel-activemq
<broker xmlns="http://activemq.apache.org/schema/core" useJmx="true"
persistent="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616" />
</transportConnectors>
</broker>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:src/data?noop=true" />
<to uri="activemq:personnel.records" />
</route>
<route>
<from uri="activemq:personnel.records" />
<log message="${body}" />
<choice>
<when>
<xpath>/person/city = 'London'</xpath>
<to uri="file:target/messages/uk" />
</when>
<otherwise>
<to uri="file:target/messages/others" />
</otherwise>
</choice>
</route>
</camelContext>
<bean id="activemq" class="org.apache.camel.component.jms.JmsComponent"
p:configuration-ref="jmsConfig" p:transacted="true" p:cacheLevelName="CACHE_CONSUMER" />
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"
p:connectionFactory-ref="nakedConnectionFactory"
p:concurrentConsumers="20" p:maxConcurrentConsumers="500"
p:idleConsumerLimit="10" />
<bean id="nakedConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
p:brokerURL="tcp://localhost:61616" />
I am unable to split Java objects that are in a List. How I can tokenize / convert Body to to single java objects?
<route id="cleanupMigratedFiles" autoStartup="true">
<from uri="timer://kickoff?period=5s" />
<bean ref="migrationProcessor" method="getCacheDeleteObjects" /> <!-- this gives me a List-of-CacheMigr -->
<log message="\n\t########\n\tCleanupMigrated file: ${body}" />
<pipeline>
<split>
<tokenize /> <!-- How to tokenize a List-of-CacheMigr -->
<convertBodyTo type="era.oddw.entity.CacheMigr" /> <!-- Do I need this? -->
<log message="\n\t########\n\tCleanupMigrated file: ${body}" />
</split>
</pipeline>
</route>
Found the answer after some more reading .. The following tag tokenized List properly : ${body}
Thanks to Camel guys.
<split streaming="true">
<simple>${body}</simple>
<convertBodyTo type="era.oddw.entity.CacheMigr" />
<log message="\n\t########\n\tCleanupMigrated file each: ${body}" />
</split>