Apache camel, create routes dynamically
Create camel routes dynamically
In this entry we'll create a camel route dynamically.The route uses the camel-mail component that permits to connect to pop/imap/smtp mailboxs.
Dependencies
We use the version 2.9.0 of camel.maven
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-mail</artifactId> <version>2.9.0</version> </dependency>ivy
<dependency org="org.apache.camel" name="camel-core" rev="2.9.0"/> <dependency org="org.apache.camel" name="camel-mail" rev="2.9.0"/>
RouteBuilder
In order to create a new Route it is necessary to create a class that extends RouteBuilder.After you have to implement the method configure() that permits to configure your route.
Example for an imap email route
 public class MailRouteBuilder extends RouteBuilder {
 
  private String login;
  private String password;
  private String host;
  public MailRouteBuilder(String login,String password,String host){
   this.login=login;
   this.password=password;
   this.host=host;
  }
  @Override
  public void configure() throws Exception {
   fromUri=String.format("imap://%s?username=%s&password=%s",host,login,password);
   from(fromUri).to("mock:myMock");
  }
 }
If you use a processor, don't forget to assign an id to the bean inside the camel spring context ex: from(fromUri).processRef("imapProcessor").to("mock:myMock");
<bean id="imapProcessor" class="myPackage.MyProcessor" />Ok, now we have a builder for the new route in a very easy way.
Now it is necessary to create a new spring bean that implements CamelContextAware. If you use spring and annotation you can configure in a very easy way with the @Service annotation:
 @Service
 public class CamelRouteManager implements CamelContextAware { ...
  
If you don't use the annotation, so you have to edit the camel spring context:
<bean id="camelRouteManager" class="myPackage.CamelRouteManager" />The implementation of CamelContextAware need to implement the getter/setter for the camel context. It means you have to provide a camelContext field.
 protected CamelContext camelContext;
 public CamelContext getCamelContext() {
  return camelContext;
 }
 public void setCamelContext(CamelContext camelContext) {
  this.camelContext = camelContext;
 }
Now we can create a method named addMailRoute that creates a new imap route: public void addMailRoute(String login,String password,String host){
  MailRouteBuilder mailRouteBuilder=new MailRouteBuilder(login,password,host);
  this.camelContext.addRoutes(mailRouteBuilder); 
 }
If all is ok the route should start automatically.Ok, but if I want to remove, stop or suspend a route, how can I do ?
Very easy, you have just to use:
camelContext.removeRoute(String routeId)
camelContext.stopRoute(String routeId)
camelContext.suspendRoute(String routeId)Take care, for remove a route it is necessary to stop it before.
Ok, how can I retrieve the id of the route created dynamically ?
Just have to retrieve the routeDefinition object, go back in the MailRouteBuilder class:
 private RouteDefinition routeDefinition;
 
 @Override
 public void configure() throws Exception {
  fromUri=String.format("imap://%s?username=%s&password=%s",host,login,password);
  routeDefinition=from(fromUri).to("mock:myMock");
 }
 public RouteDefinition getRouteDefinition() {
  return routeDefinition;
 }
 
Now retrieve the routeDefinition object and extract the id in the CamelRouteManager class: public String addMailRoute(String login,String password,String host){
  MailRouteBuilder mailRouteBuilder=new MailRouteBuilder(login,password,host);
  this.camelContext.addRoutes(mailRouteBuilder);
  return mailRouteBuilder.getRouteDefinition().getId(); 
 }
It is also possible to create several routes in one shot. You can adapt the code to have a list of routeDefinition instead of a simple StringNow all is done, you can enjoy all your created routes.
apache camel official site
 
