uttam
uttam

Reputation: 455

AutoGenerating routeId's in camel

In apache-camel, is there a way to auto generate routeId's overriding the existing ones with route numbers(generated in RouteDefinitionHelper)?

Upvotes: 5

Views: 730

Answers (4)

b_habegger
b_habegger

Reputation: 306

If you are defining your routes using the java RouteBuilder you could subclass RouteBuilder and Override the from (and any other route building methods method):

public abstract class MyRouteBuilder extends RouteBuilder {
    @Override
    public RouteDefinition from(String uri) {
       return super.from(uri).id("route:"+uri);
    }
}

Upvotes: 0

timtish
timtish

Reputation: 49

For spring-camel You can use this code:

@Component
public class UriNodeIdFactory extends DefaultNodeIdFactory {
@Override
public String createId(NamedNode def) {
    String key = def instanceof NamedRoute nr ? nr.getEndpointUrl()
       : def instanceof EndpointRequiredDefinition ed ? ed.getEndpointUri()
       : def.getShortName();
    if (key.contains("{{") && def instanceof OptionalIdentifiedDefinition od && od.getCamelContext() != null) {
        key = od.getCamelContext().resolvePropertyPlaceholders(key);
    }
    return key.replace(":", "-").replace(".", "_") + getNodeCounter(key).incrementAndGet();
}
}

Upvotes: 0

Matthew Fontana
Matthew Fontana

Reputation: 3870

There is to the best of my knowledge no autoGeneration policy on routeNaming you can use, but you could do something similar to this:

private String myURI;

from("jms:queue:" + myURI).routeId("JmsComponent:" + myURI)
    .to("....");

By using something like blueprint or spring to inject your variable to the java class you can change your URI and it will adjust the route name accordingly. You could also use the full URI in your private variable then parse the endpointURI yourself and format it for the routeId.

Upvotes: 1

Matthew Fontana
Matthew Fontana

Reputation: 3870

You can specify them directly for routes as well as processors in your routes.

from("direct:start").routeId("MyMainRoute")
    .to("direct:out").id("MyOutputProcessor");

These ids will be visible in your jConsole so you can see statistics on your routes and processors as well.

Upvotes: 0

Related Questions