As of camel 2.11.x, the simple expression has a new variable called camelContext which can be used to evaluate if a route exists camelContext.hasEndpoint.
<choice>
<when>
<description>Check if a country specific handler is available</description>
<simple>${camelContext.hasEndpoint(direct:${header.operationName}${body[0].country})} != null</simple>
<recipientList>
<description>country specific handler is available</description>
<simple>direct:${header.operationName}${body.country}</simple>
</recipientList>
</when>
<otherwise>
<recipientList>
<description>country specific handler is not available then route to generic processing</description>
<simple>direct:${header.operationName}</simple>
</recipientList>
</otherwise>
</choice>
For those using an earlier version of apache-camel like 2.10.x, you can use camel's context to check if a route exists via the exchange object and its Dynamic router
Route info,
<dynamicRouter>
<method ref="dynamicRouterService" method="route"/>
</dynamicRouter>
The Dynamic router,
public class DynamicRouterService {
public String route(@Header("operationName") String operationName, @Properties Map<String, Object> properties, Exchange exchange, Country body) {
//needed to end the iterative routing calls
if (Boolean.valueOf((String)properties.get("SERVICE_INVOKED"))) {
return null;
}
//indicate that the route has been invoked
properties.put("SERVICE_INVOKED", "true");
return this.orchestrateRoute(exchange, operationName, body.getcountry());
}
private String orchestrateRoute(Exchange exchange, String operationName, String country) {
String uriBaseString = "direct:" + operationName;
String uriCountryString = uriBaseString + country;
Endpoint endpoint = exchange.getContext().hasEndpoint(uriCountryString);
if (endpoint != null) {
return uriCountryString;
} else {
return uriBaseString;
}
}
}
Thanks Claus for pointing to the camel context