The camel route adding method is generally implemented by calling the addroutes (routesbuilder builder) method of camelcontext. Let's take a look at how this method adds a route:
Public void addroutes (routesbuilder builder) throws exception {// call the addroutestocamelcontext method of routebuilder and pass camelcontext as a parameter to builder. addroutestocamelcontext (this );}
The following is the source code of the addroutestocamelcontext method:
public void addRoutesToCamelContext(CamelContext context) throws Exception { configureRoutes((ModelCamelContext)context); populateRoutes();}
Here we call two methods. Let's first look at the assumeroutes method. The work of this method is to configure the route:
public RoutesDefinition configureRoutes(ModelCamelContext context) throws Exception { setContext(context); checkInitialized(); routeCollection.setCamelContext(context); return routeCollection;}
The most important of this is the checkinitialized method. The others are very simple. Let's take a look at the checkinitialized method:
protected void checkInitialized() throws Exception { if (initialized.compareAndSet(false, true)) { ModelCamelContext camelContext = getContext(); if (camelContext.getErrorHandlerBuilder() != null) { setErrorHandlerBuilder(camelContext.getErrorHandlerBuilder()); } configure(); for (RouteDefinition route : getRouteCollection().getRoutes()) { route.markPrepared(); } }}
This method is first checked for initialization. If it is the first execution, it is certainly not initialized. If it will execute the configure () method, this method is implemented when we create a routebuilder object. In this method, the configuration and creation of routedefinition are completed. After the configure () method is executed, all route entries are traversed and marked as pre-processed.
Next let's take a look at the populateroutes () method. In this method, we will add the routing definition to the camelcontext:
Protected void populateroutes () throws exception {modelcamelcontext camelcontext = getcontext (); If (camelcontext = NULL) {Throw new illegalargumentexception ("camelcontext has not been injected! ");} Getroutecollection (). setcamelcontext (camelcontext); // remove all route definitions and add them to camelcontext. addroutedefinitions (getroutecollection (). getroutes ());}
The following is the addroutedefinitions source code of camelcontext:
Public synchronized void addroutedefinitions (collection <routedefinition> routedefinitions) throws exception {// traverse all route definitions for (routedefinition: routedefinitions) {// if the route definition already exists, remove the original definition removeroutedefinition (routedefinition);} // Add all the route definitions to this in the route definition set. routedefinitions. addall (routedefinitions); // determines whether to start the route if (shouldstartroutes () {startroutedefinitions (routedefinitions );}}
In the shouldstartroutes method, true is returned if camelcontext is started and not during startup. Therefore, after camelcontext is started, adding a new route definition will automatically start the route definition.
After creating the route and drop configurations, the following is the most important process: the execution process of the route definition, that is, the START process of the route definition ......
Route adding process for camel