Intercepting Exchange in Camel

816 views Asked by At

Assume I have the route as follows

from("direct:A")
  .process(new ProcessA())
  .setHeader(Exchange.HTTP_METHOD, "get")
  .recipientList( simple(httpUri + header("doc_id")), "false")
  .process(new ProcessB())
  .to("direct:B");

In the above path httpUri = "http4://localhost:25600". Now I am trying to intercept the message as follows.

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
         interceptSendToEndpoint("http4*")
         .skipSendToOriginalEndpoint()
         .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
               //TODO                                 
            }
         });
    }
});

The problem here is that the exchange is not being intercepted and context is actually trying to make a connection with httpUri host even there is skipSendToOriginalEndpoint.

Please let me know if there is anything wrong in the code. Thanks in advance.

1

There are 1 answers

0
Ray On BEST ANSWER

You are using a dynamic route, which means that the endpoint is not known at definition-time. As a result you aren't going to be able to intercept based on the http endpoint.

What I would do instead is swap out the whole recipientList, rather than just the http destination called by the recipientList.

As simple way to do this using weaveByType. To weave in your processor, this would look like:

weaveByType(RecipientListDefinition.class).replace().process(...)

As always with adviceWith, make sure to implement isUseAdviceWith to return true, and to then call context.start() after advicing the routeDefinition.