Camel route construction process

Source: Internet
Author: User
I personally think that the two most important parts of camel are the routing construction process and the execution process after the routing construction.
The following uses the preceding camel example to describe how to build a route.
In fact, the route construction mentioned here is actually to build the route definition, which corresponds to the routedefinition class in the camel. A routedefinition object specifies or specifies the process in which a message is generated and what is going through, the destination of the route. Routedefinition is a bit similar to the class in Java. It contains metadata, which is used by external parties for reference. Routedefinition is built by routebuilder. The specific point is to call the configure () method of routebuilder and add it to the camelcontext after the build is complete. Then the route definition can be run. The configure () method of routebuilder is an abstract method. Therefore, this method must be implemented by developers. In fact, it is the process of creating a route definition. The code in configure () in the preceding camel example is as follows:

camelContext.addRoutes(new RouteBuilder() {@Overridepublic void configure() throws Exception {this.from("file:H:/temp/in").process(new Processor() {@Overridepublic void process(Exchange exchange) throws Exception {GenericFile<File> gf = exchange.getIn().getBody(GenericFile.class);File file = gf.getFile();PrintStream ps = new PrintStream(System.out);BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));String line = null;while((line=br.readLine())!=null) {ps.println(line);}ps.close();br.close();}}).to("file:H:/temp/out");}});

The start of a route definition is to call the from method of routebuilder. The from method has multiple overload methods. One is to pass in a URI string, and the other is to pass in an endpoint object, we can see that this form of overload should be able to think that, given a URI string, it is also necessary to obtain an endpoint object, in the camel component search method described in the previous article, camel needs to parse the component name based on the URI, and then find the component by the component name. In the important concepts of camel, A component is actually an endpoint, and its role is to create an endpoint object. Here, camel obtains the endpoint object based on the URI string. This line should be passed.

Next we will look at a routebuilder's from (URI) method, because this method is most commonly used:

public RouteDefinition from(String uri) {getRouteCollection().setCamelContext(getContext());RouteDefinition answer = getRouteCollection().from(uri);configureRoute(answer);return answer;}

First, call the getroutecollection () method. This method returns a routesdefinition object, which represents a set of multiple route definitions, because multiple route definitions can be run in camel. Set the camelcontext object to routesdefinition, and then call the from (URI) method of routesdefinition:

Public routedefinition from (string URI) {// create routedefinition object routedefinition route = createroute (); route. from (URI); Return Route (route);} protected routedefinition createroute () {// create routedefinition object routedefinition route = new routedefinition (); errorhandlerfactory handler = callback (); if (handler! = NULL) {// set the error processor route. seterrorhandlerbuilderifnull (handler);} return route;} For routedefinition ;}

After routedefinition is created, the from (URI) method of routedefinition is called:

Public routedefinition from (string URI) {// create a fromdefinition object and add it to the input of the route definition getinputs (). add (New fromdefinition (URI); // returns the current routedefinition object return this ;}

After the from (URI) method of routedefinition is called, call the Route Method of routesdefinition:

Public routedefinition route (routedefinition route) {// pre-processes routedefinition and sets routedefinitionhelper, such as exception processors and interceptors. prepareroute (getcamelcontext (), route, getonexceptions (), getintercepts (), getinterceptfroms (), getinterceptsendtos (), getoncompletions ()); // Add the pre-processed routedefinition to the routedefinition set, so that camel knows that there is an additional route definition getroutes (). add (route); // mark the route definition that has been processed. markprepared (); // return the current routedefinition object return route ;}

After the routesdefinition route method is called, call the assumeroute method. This method only defines the group attribute value for the route.
According to the above analysis, the from method of routebuilder creates a routedefinition object, adds a fromdefinition object to the input of the object, and then adds the routedefinition object to routesdefinition.

Next, call the routedefinition process method, which is defined in the processordefinition class. routedefinition inherits from processordefinition to this method. Note that the return value of this method is generic, this method receives a processor type parameter:
Public type process (processor) {processdefinition answer = new processdefinition (processor); addoutput (answer); // This Is Not A processdefinition object, but routedefinition object return (type) this ;}

This method is simple. Create a processdefinition object and add it to the routedefinition output (outputs). Finally, return the routedefinition object.
Next, call the To method of routedefinition. This method also inherits from processordefinition, And the return value is also generic:

public Type to(String uri) {    addOutput(new ToDefinition(uri));    return (Type) this;}

This method is also very simple. Create a todefinition object and add it to the routedefinition output (outputs.

Now, in this simple example, the entire routing definition is built. In fact, the most important thing about the routing definition is the input and output, and there can be multiple inputs and outputs, by default, after a route is run, camel will call these output processors and route the messages to the specified destination.

Camel route construction process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.