You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2012/12/03 10:21:49 UTC

Re: Transactions, Error, Stop route

Hi

Setting a longer redelivery delay on the broker side may help to stop
the route, without having the message being redelivered so quickly.

Also you are using handled(true) which you should not do. Otherwise
the transaction will commit, as the failure was handled.


On Fri, Nov 30, 2012 at 10:13 AM, Praan <pa...@euronet.nl> wrote:
> Dear All,
>
> I would like to discuss/have some advice on the following:
> Functional requirement:
> In case an error occurs in our route while while processing a message from
> ActiveMQ queue.
> We want the message to return back to the queue and the route to stop
> working.
>
> Technically we think we need a combination of Transaction, onException
> handeling and a way to stop the route from further processing/running.
>
> This results in the following code:
>
> Camel transaction/activemq related configuration:
>     <bean id="jms"
> class="org.apache.activemq.camel.component.ActiveMQComponent">
>         <property name="transacted" value="true"/>
>         <property name="transactionManager" ref="txManager"/>
>         <property name="connectionFactory" ref="jmsConnectionFactory"/>
>     </bean>
>
>     <bean id="txManager"
> class="org.springframework.jms.connection.JmsTransactionManager">
>         <property name="connectionFactory" ref="jmsConnectionFactory" />
>             </bean>
>
>     <bean id="jmsConnectionFactory"
> class="org.apache.activemq.ActiveMQConnectionFactory">
>         <property name="brokerURL" value="tcp://localhost:61616"/>
>     </bean>
>
>     <bean id="txPolicy"
> class="org.apache.camel.spring.spi.SpringTransactionPolicy">
>         <property name="transactionManager" ref="txManager"/>
>         <property name="propagationBehaviorName"
> value="PROPAGATION_REQUIRED"/>
>     </bean>
>
> //stop the route in the onException
> onException(IOException.class)
>                 .process(new Processor() {
>                     Thread stopRouteInAnotherThread;
>
>                     @Override
>                     public void process(final Exchange exchange) throws
> Exception {
>                         // stopRouteInAnotherThread this route using a
> thread that will stopRouteInAnotherThread
>                         // this route gracefully while we are still running
>                         if (stopRouteInAnotherThread == null) {
>                             stopRouteInAnotherThread = new Thread() {
>                                 @Override
>                                 public void run() {
>                                     try {
>
> exchange.getContext().stopRoute("poc-transaction");
>                                     } catch (Exception e) {
>                                         // ignore
>                                     }
>                                 }
>                             };
>                         }
>
>                         // start the thread that stops this route
>                         stopRouteInAnotherThread.start();
>                     }
>                 })
>                 .maximumRedeliveries(0)
>                 .handled(true);
> Simple Route:
> from("jms:queue:myQueueStart")
>                 .routeId("poc-transaction")
>                 .transacted(TX_POLICY_SPRING_BEAN)
>                 .process(new Processor() {
>                     @Override
>                     public void process(Exchange exchange) throws Exception
> {
>                         throw new IOException("test");
>                     }
>                 }).to("jms:queue:myQueueEnd");
>
> The route is stopped though we expected the message not to be dequeue'd but
> it is dequeue'd.
>
> Does anyone know how to achieve this?
>
> Cheers,
> Praan
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Transactions-Error-Stop-route-tp5723438.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Transactions, Error, Stop route

Posted by Praan <pa...@euronet.nl>.
The above code works. It results in the route being rollbacked. The message
is kept on the queue, and our bundle is stopped. 

Though what we don't understand is the position and roll of the
markRollbackOnly(). 

We expected to use this. But putting the markRollbackOnly after the
onException(IOException.class).markRollbackOnly(), it shows different
behaviour when putting it at the end of the onException(..).

It even seems that we don't need it anyway...



--
View this message in context: http://camel.465427.n5.nabble.com/Transactions-Error-Stop-route-tp5723438p5723591.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Transactions, Error, Stop route

Posted by Praan <pa...@euronet.nl>.
Okay we have got it working:

Transaction configuration:
   <bean id="jms"
class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="transacted" value="true"/>
        <property name="transactionManager" ref="txManager"/>
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
    </bean>

    <bean id="txManager"
class="org.springframework.jms.connection.JmsTransactionManager">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
            </bean>

    <bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>

    <bean id="txPolicy"
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
        <property name="transactionManager" ref="txManager"/>
        <property name="propagationBehaviorName"
value="PROPAGATION_REQUIRED"/>
    </bean>

Sample Route with onException:
Note: make the camel route BundleContextAware (by implementing
BundleContextAware)

//act upon the (error) cause of the rollback
        onException(IOException.class)
                .process(new Processor() {
                    //create a new thread to stop the bundle.
                    //Stopping the route in the same 'running' thread
                    //might result in infinite wait
                    Thread stopRouteInAnotherThread = null;

                    @Override
                    public void process(final Exchange exchange) throws
Exception {
                        // stopRouteInAnotherThread this route using a
thread that will stopRouteInAnotherThread
                        // this route gracefully while we are still running
                        if (stopRouteInAnotherThread == null) {
                            stopRouteInAnotherThread = new Thread() {
                                @Override
                                public void run() {
                                    try {
                                        Bundle bundle =
bundleContext.getBundle();
                                        bundle.stop();
                                    } catch (BundleException e) {
                                        LOG.error("Failed to stop bundle: "
+ e.getMessage());
                                    }
                                }
                            };
                        }

                        // start the thread that stops this route
                        stopRouteInAnotherThread.start();
                    }
                })
                .useOriginalMessage();

from("jms:queue:praan")
                .routeId("YOUR_ROUTE_ID")
                .transacted("txPolicy")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception
{
                        if
(exchange.getIn().getBody(String.class).contains("Deepika")) {
                            throw new IOException("test exception to break
route");
                        }
                    }
                }).to("jms:queue:praan_out");



--
View this message in context: http://camel.465427.n5.nabble.com/Transactions-Error-Stop-route-tp5723438p5723590.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Transactions, Error, Stop route

Posted by Praan <pa...@euronet.nl>.
Hai Claus,

Thanks for your reply.

Yes removing handled(true) makes sure that the transaction is rolledBack and
the message is kept on the queue. Thanks!

The route also stops working though we prefer the feature to be uninstalled
from Karaf (OSGI). 

We are looking into getting the BundleContext and try to uninstall it via
this. 

Do you have any experience with de-installing the feature/bundle via Camel? 



--
View this message in context: http://camel.465427.n5.nabble.com/Transactions-Error-Stop-route-tp5723438p5723528.html
Sent from the Camel - Users mailing list archive at Nabble.com.