How to check if a apache camel route exists? -


i trying find if direct route exists in camel of try catch block below. looking predicate check if route exists in camel or not. not find directly gives me answer, took below approach,

<dotry>     <recipientlist>         <description>check if country specific handler available</description>          <simple>direct:${header.operationname}${body.country}</simple>      </recipientlist>     <docatch>         <exception>org.apache.camel.component.direct.directconsumernotavailableexception</exception>          <recipientlist>             <description>if country specific handler not available to base</description>              <simple>direct:${header.operationname}</simple>          </recipientlist>     </docatch> </dotry> 

this means forced use exception handler in camel catch directconsumernotavailableexception determine if route exists. looking @ alternate approach can use simple expression exists below,

<choice>     <when>         <description>check if country specific handler available</description>         <simple>direct:${header.operationname}${body.country} exists</simple>         <recipientlist>             <description>country specific handler available</description>             <simple>direct:${header.operationname}${body.country}</simple>         </recipientlist>     </when>     <otherwise>         <recipientlist>             <description>country specific handler not available route generic processing</description>             <simple>direct:${header.operationname}</simple>         </recipientlist>     </otherwise> </choice> 

please let me know if can achieved using other means.

as of camel 2.11.x, simple expression has new variable called camelcontext can used evaluate if route exists camelcontext.hasendpoint.

<choice>     <when>         <description>check if country specific handler available</description>         <simple>${camelcontext.hasendpoint(direct:${header.operationname}${body[0].country})} != null</simple>         <recipientlist>             <description>country specific handler available</description>             <simple>direct:${header.operationname}${body.country}</simple>         </recipientlist>     </when>     <otherwise>         <recipientlist>             <description>country specific handler not available route generic processing</description>             <simple>direct:${header.operationname}</simple>         </recipientlist>     </otherwise> </choice> 

for using earlier version of apache-camel 2.10.x, can use camel's context check if route exists via exchange object , 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 end iterative routing calls         if (boolean.valueof((string)properties.get("service_invoked"))) {             return null;         }          //indicate 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 pointing camel context


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -