rapt
rapt

Reputation: 12230

Camel: getting EndpointUri of a nested route

I have route A that calls route B:

from("direct:a")
.to("direct:b");

from("direct:b")
.process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception
        {
            System.out.println("Route B URI: " + exchange.getUnitOfWork().getRouteContext().getFrom().getEndpointUri()); //oops, prints direct:a
        }
    });

I would like the nested route B to print out its own URI, not the URI of the encapsulating route A. How could I do it?

Upvotes: 0

Views: 293

Answers (2)

Sylvain Deschenes
Sylvain Deschenes

Reputation: 1236

Add a routeId() instruction to your route and use that to access your route definition:

from("direct:b")
   .routeId("routeB")
   .process(new Processor() { 
          @Override 
           public void process(Exchange exchange) throws Exception { 
                System.out.println("Route B URI: " + exchange.getContext().getRoute("routeb").getFrom().getEndpointUri());  
           }
    });

Upvotes: 0

Jérémie B
Jérémie B

Reputation: 11032

Afaik, I don't think it's possible as is.

The UnitOfWorks keep a Stack of RouteContext, but this stack is not publicly accessible. However, you can access a history of the processor in which the exchange has been routed. This collection of MessageHistory is located in an header Exchange.MESSAGE_HISTORY. It contains the id of the processor and (sanitized) uri of the endpoints.

A more simple/robust approach can be to add explicitly a header on each route invoking the route B.

Personally, I don't think it's a good idea to depend on such information // internal details of the route!

Upvotes: 2

Related Questions