routesapache-camelinterceptorpost-processing

How to add postprocess interceptor in apache camel with condition?


I have 4 days experience with apache camel and hence this question.

What I would like to do is to add interceptor that would be triggered AFTER endpoint if condition met.

I am able to just add AFTER endpoint interceptor by doing this:

interceptSendToEndpoint(ENDPOINT_1)
                .skipSendToOriginalEndpoint()                
                .to(ENDPOINT_1)
                .afterUrl(AFTER_ENDPOINT_1);

I would assume that if I would like to add condition to this chain I would write this:

interceptSendToEndpoint(ENDPOINT_1)
                    .skipSendToOriginalEndpoint()
                    .choice()
                    .when(conditionIsMet).to(ENDPOINT_1).afterUrl(AFTER_ENDPOINT_1)
                    .otherwise(ENDPOINT_1);

But apache camel's API thinks different. ;) (like apple)

So the logic I am trying to achieve is: If condition met - then AFTER_ENDPOINT_1 should be executed, if condition is NOT met then AFTER_ENDPOINT_1 should NOT be executed.

Could you please assist with what should I write in this scenario? Perhaps I misuse interceptors - in this case, please direct me to the right path.


Solution

  • After 2 days of tests I came to conclusion that the best way to do it is this:

      interceptSendToEndpoint(ENDPOINT_2)
                    .when(conditionMet)
                    .skipSendToOriginalEndpoint()
                    .to(ENDPOINT_2)
                    .afterUrl(MY_AFTER_URI);
    

    With such route:

    from(MY_ROUTE_EXMPL)
                        .log("after MY_ROUTE_EXMPL log")
                        .to(ENDPOINT_1)
                        .log("after ENDPOINT_1")
                        .to(ENDPOINT_2)
                        .log("after ENDPOINT_2")
                        .to(RETURN_STRING_ENDPOINT)
                        .log("after RETURN_STRING_ENDPOINT");
    

    with configuration this is the output when condition MET:

    2021-10-11 01:40:05.322  INFO 2059 --- [nio-8990-exec-7] route3                                   : after MY_ROUTE_EXMPL log
    inside: com.camel.interceptor.bean.TestConsumer.endpoint1
    2021-10-11 01:40:05.323  INFO 2059 --- [nio-8990-exec-7] route3                                   : after ENDPOINT_1
    inside: com.camel.interceptor.bean.TestConsumer.endpoint2
    inside: com.camel.interceptor.bean.TestConsumer.afterUri
    2021-10-11 01:40:05.323  INFO 2059 --- [nio-8990-exec-7] route3                                   : after ENDPOINT_2
    inside : com.camel.interceptor.bean.TestConsumer.returnString
    2021-10-11 01:40:05.323  INFO 2059 --- [nio-8990-exec-7] route3                                   : after RETURN_STRING_ENDPOINT
    

    The TO endpoints are represented as simple @Consume annotated methods of void type with only single

    System.out.println("inside: com.camel.interceptor.bean.TestConsumer.{ENDPOINT_METHOD_NAME}");
    

    call.

    When condition is NOT met then output is:

    2021-10-11 01:44:15.005  INFO 2059 --- [io-8990-exec-10] route3                                   : after MY_ROUTE_EXMPL log
    inside: com.camel.interceptor.bean.TestConsumer.endpoint1
    2021-10-11 01:44:15.005  INFO 2059 --- [io-8990-exec-10] route3                                   : after ENDPOINT_1
    inside: com.camel.interceptor.bean.TestConsumer.endpoint2
    2021-10-11 01:44:15.005  INFO 2059 --- [io-8990-exec-10] route3                                   : after ENDPOINT_2
    inside : com.camel.interceptor.bean.TestConsumer.returnString
    2021-10-11 01:44:15.005  INFO 2059 --- [io-8990-exec-10] route3                                   : after RETURN_STRING_ENDPOINT
    

    Both output are as expected.

    As stated in the question: Now when condition is MET then execution goes into ENDPOINT_2 (only once) AND AFTER_URI.

    When condition is NOT MET then execution goes ONLY in ENDPOINT_2 WITHOUT hitting AFTER_URI.

    Here is link to repo with examples of different aproaches of interception. Those examples are made by my colleague Tarun, I only added one more example that looks as a best option to me and the same example I present here as an answer. Thank you Tarun for your help.

    Here is the link to source code with examples: https://github.com/ffatheranderson/taruns_apache_camel_examples