3

How do I stop a camel route when there is an exception. My dynamic route is consuming jms and sending to reactive endpoint.

camelContext.addRoutes(New GenerateRoute());

Route generator class is as below:

public class GenerateRoute extends RouteBuilder {

@Override
public void configure() {
    from("jms:queue:myQueue")
        .toF("reactive-streams:myStream").setId("myRoute");
}}
user3549576
  • 99
  • 1
  • 4
  • 17

2 Answers2

2

You can use handled and continued. in the onException clause. "Continued allows you to both handle and continue routing in the original route as if the exception did not occur." if continued is false the routing won't go back to the original route.

DSL:

<onException>
    <exception>java.lang.IllegalArgumentException</exception>
    <continued><constant>false</constant></continued>
</onException>

Java:

onException(IllegalArgumentException.class).continued(fasle);

Refer to: https://camel.apache.org/manual/latest/exception-clause.html#

  • Will this terminate only the generated route correct?I updated my question with the code I have. Does this look right as below? – user3549576 Jul 01 '20 at 11:45
  • `public class GenerateRoute extends RouteBuilder { onException(Exception.class).process(exchange -> { System.out.println("handling ex"); }).continued(false); @Override public void configure() { from("jms:queue:myQueue") .toF("reactive-streams:myStream").setId("myRoute"); } }` – user3549576 Jul 01 '20 at 11:50
  • Your question is unclear to me. What `continued` does is in handling the exception. If you want to return the control back to the route then the continued is true otherwise you can set it false. If `handled` is true the occurred exception will be removed in the exchange otherwise it will remain in the exchange and you can still retrieve it from exchange using: `Throwable caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);` These are – Poorya Hosseini Jul 01 '20 at 12:47
  • I can give and example: if the message is read from queue and send to another route like `direct:enrich-message` which enriches the message and then from`direct:enrich-message` you sent it to `direct:send-to-mysystem` where you send it to`reactive-streams:myStream` if you have `continued=true` and `handled=false` in the `onException` clause of `direct:send-to-mysystem` then the exchange will be sent back to `direct:enrich-message` route with the caught exception in the exchange. – Poorya Hosseini Jul 01 '20 at 13:00
  • I am looking for a way to completely stop the route so the route doesn't consume from JMS anymore. Basically context.getRoute("myRoute").getConsumer.stop(). As I understand continued(false) will still keep consuming from JMS. Will transacted work in this case? – user3549576 Jul 01 '20 at 13:06
  • Then you need to stop the whole route. See: https://camel.apache.org/manual/latest/faq/how-can-i-stop-a-route-from-a-route.html – Poorya Hosseini Jul 01 '20 at 13:10
  • Moreover, I think you need to change your question as well. – Poorya Hosseini Jul 01 '20 at 13:11
  • See also this: https://camel.apache.org/manual/latest/lifecycle.html – Poorya Hosseini Jul 01 '20 at 13:26
2

Try this

    from("jms:queue:myQueue")
            .routeId("myRoute")
            .doTry()
                .toF("reactive-streams:myStream")
            .doCatch(Exception.class)
                .process(exchange -> exchange.getFromEndpoint().stop())
            .end();
  • You mean doCatch() instead of doFinally() correct? Since I need to stop the route only when there is an exception. – user3549576 Jul 01 '20 at 13:34
  • Thank you! Also, I believe this will still loose one message from jms queue correct? If yes, is there a way to add "transacted"? – user3549576 Jul 01 '20 at 13:43
  • ok thank you! one more question... is the .routeId at correct place if I want to start the route again at later point? – user3549576 Jul 01 '20 at 16:31
  • How do I add transaction in this so that the message that caused the exception is not consumed and stays in the jms queue? – user3549576 Jul 05 '20 at 14:59
  • I have added followup question here https://stackoverflow.com/questions/62743621/how-to-setup-transaction-in-camel-jms-route – user3549576 Jul 05 '20 at 16:50