Important Concepts in camel

Source: Internet
Author: User
Endpoint: the node where the message passes
Endpoint supported by camel
1. JMS queue
2. Web Service
3. File
4. FTP service
5. Email Address
6. pojo
In camel-based applications, you will create some endpoints and connect them with routes. Camel defines an endpoint interface. Each endpoint supported by camel has a class that implements this endpoint interface, camel provides a separate javadoc inheritance tree for each communication technology supported by camel.

Camelcontext
A camelcontext object represents the camel runtime system. Typically, you have a camelcontext object in an application. A typical application follows the following steps:
1. Create a camelcontext object
2. Add endpoints and possible components to the camelcontext object.
3. Add a route to the camelcontext object to connect to these endpoints
4. Call the START () method of camelcontext to start internal camel threads. These threads are used to process the sending, receiving, and processing of messages in the endpoint.
5. Call the stop () method of camelcontext to stop all internal camel threads.

Note: The camelcontext. Start () method is not blocked without restriction. Instead, the method starts the internal thread for each component and endpoint and returns the result. Similarly, the camelcontext. Stop () method will not return until all internal threads of each component and Endpoint are terminated.
If the camelcontext. Start () method is not called in your application, the message will not be processed because the internal thread has not been created. If you did not call the camelcontext. Stop () method before stopping your application, the application may stop in an inconsistent state. If the camelcontext. Stop () method is not called in the JUnit test, the test may fail because the message has no chance to be completely processed.

Cameltemplate
Camel used to have a class named camelclient, but now it has changed its name to cameltemplate. The reason for this is to cater to naming conventions of other open-source projects, such as transactiontemplate and jmstemplate in spring.
The cameltemplate class is a streamlined class that encapsulates camelcontext. It can send messages or exchange messages to endpoints. This provides a way to input a message to the source endpoint, so the message will move along the route until the destination endpoint.


Component
Component is an easy term. Using endpointfactory may be more appropriate because component is a factory used to create an endpoint instance. For example, if a camel-based application uses multiple JMS queues, the application will create a jmscomponent (implemented Component Interface) class instance, and then the application will call its createendpoint () method To create an endpoint instance. In fact, application-level code does not directly call component. the createendpoint () method, which usually calls camelcontext. instead of the getendpoint () method, the camelcontext object finds the corresponding component and calls its createendpoint () method. For example, the following code:
Mycamelcontext. getendpoint ("POP3: // [email protected]? Password = mypassword ");
The parameter of the getendpoint () method is a URI. the prefix of This URI specifies the name of this component ). Internally, the camelcontext object has a ing table mapped to the component. In the URI in the preceding example, the camelcontext object may map the POP3 prefix to a mailcomponent instance. Then the camelcontext object calls the createendpoint ("POP3: // [email protected]? Password = mypassword ") method. The createendpoint () method separates the URI into several parts and uses these parts to configure the endpoint object.
In the previous section, we mentioned that a camelcontext object has a componing from component name to component object. This raises a question. There are two ways to map these components in this ing table:
First, call the camelcontext. addcomponent (string componentname, component) method at the application level. The following example registers a mailcomponent object with three different names in the ing table:
Component mailComponent = new org.apache.camel.component.mail.MailComponent();myCamelContext.addComponent("pop3", mailComponent);myCamelContext.addComponent("imap", mailComponent);myCamelContext.addComponent("smtp", mailComponent);</span>

Second, it is better to delay the initialization of the caming table in the camelcontext object. This method requires developers to follow certain conventions when implementing the component interface. For example, suppose you have written a class named com. example. myproject. foocomponent, And you want camel to automatically identify it with "foo. To achieve this, you must write a properties file named "META-INF/services/org/Apache/camel/component/foo" (No. properties file suffix), the properties only has one entry class, the value of this entry is the full path name of the component:
Class = com. example. myproject. foocomponent
If you still want COM. example. myproject. foocomponent is identified by name "bar". You have to write another properties file (named bar) in the same directory and contain the same content. Once you write these properties files, the jar package you created contains com. example. myproject. foocomponent and the corresponding properties file, and the jar package is added to classpath. In this way, you can call createendpoint ("foo :... ") method. Camel finds the" foo "properties file in classpath, obtains the value of the class entry in the file, and then creates an instance of the specified class using reflection technology.
Camel supports multiple out-of-the-box communication technologies. This Out-of-the-box Communication supports multiple classes that implement the Component Interface and their corresponding properties files, for the camelcontext object to build this named component object ing table.
In the previous section, I provided the following example to call camelcontext. getendpoint ().
Mycamelcontext. getendpoint ("POP3: // [email protected]? Password = mypassword ");
When I give this example for the first time, I say that the parameter of the getendpoint () method is a URI, the reason I said that is because camel's online help documentation and camel's source code both claim that this is a URI. In fact, this parameter is strictly a URL. This is because when camel extracts the component name from the parameter, it first looks for ":", which is a simple algorithm. To understand the cause, you have to go back to section4.4 ("the meaning of URL, Uri, urn and Siri"). a uri may be a URL or urn. Consider the following getendpoint () method call:
Mycamelcontext. getendpoint ("POP3 :...");
Mycamelcontext. getendpoint ("JMS :...");
Mycamelcontext. getendpoint ("urn: FOO :...");
Mycamelcontext. getendpoint ("urn: bar :...");
In the preceding example, camel identifies these components with the following names: "POP3", "JMS", "Urn", and "Urn ". if the last two components are identified with "urn: foo" and "urn: bar, in fact, we use "Urn" and "Urn" (only take the first ":" part before the ":" As the component name ). Therefore, you must use a URL ("<schema> :... format ") to identify an endpoint instead of using an urn (" urn: <schema> :... "format ). Because there is no more support for urn, the parameter of the getendpoint () method is a URL rather than a so-called Uri.

Message and exchange
The message interface is a message abstraction, such as a request, a reply, or an exception message.
Camel provides a message interface implementation class for each supported communication technology. For example, the jmsmessage class provides the message interface Implementation of JMS-specific. The common interface message provides the getter and setter methods to access the Message ID, message break, and message header fields.
The exchange interface is an abstraction of message exchange. A request message and its corresponding response or exception message. In camel technology, request, response, and exception messages are called input (in), output (out), and error (fault) messages respectively.
Camel also provides exchange interface implementation for the first supported communication technology. For example, the jmsexchange class provides the exchange interface Implementation of JMS-specific. The APIs of the exchange public interface have great limitations, which are intentionally designed to be like this. Because each implementation class that implements the exchange interface will provide specific technical methods.
Programmers at the application level seldom directly access the exchange interface (or its implementation class ). However, many classes in cmael are generic classes are instantiated on (a class that implements) exchange. The reason for this is that the exchange interface appears in many generic classes and methods.

Processor
The processor interface represents a class that can process messages. Its signature is as follows:
Package org. Apache. Camel;
Public interface processor {
Void process (exchange) throws exception;
}
Note that the parameters of the process () method are an exchange object rather than a message object. This provides scalability. For example, the process () method of an implementation class may initially call the exchange. getin () method to obtain and process the input message. If an error occurs during processing, you can call the exchange. setexception () method. An application-level developer may develop a class to implement the process interface to execute a business logic. Then many classes in the camel class library already implement the processor interface to support the design mode in EIP book. For example, choiceprocessor implements message routing.
It uses a cascade if-then-else statement to route a message from the queue to one of multiple output queues. Another example is the filterprocessor class, which will discard messages that do not meet the conditions.

Routes, routebuilders and Java DSL
A route is used to move a message from an input queue step by step to a destination queue through any type of decision. Camel provides two ways for developers to specify routes for applications. One is to configure routing information in an XML file, another method is the so-called Java DSL (domain-specific language) in camel ).

Introduction to Java DSL
For many people, the word "domain-specific language" means that a compiler or interpreter can process an input file containing keywords and specific semantics to a specific domain. However, this is not the method used by camel. The camel document insists on replacing "DSL" with "Java DSL", but this cannot completely avoid potential confusion. Camel's "Java DSL" is a class library that can be used in a similar way as DSL unless it contains a small number of Java syntaxes. You can refer to the following example. The annotations below explain the composition of this example.
RouteBuilder builder = new RouteBuilder() {    public void configure() {        from("queue:a").filter(header("foo").isEqualTo("bar")).to("queue:b");        from("queue:c").choice()                .when(header("foo").isEqualTo("bar")).to("queue:d")                .when(header("foo").isEqualTo("cheese")).to("queue:e")                .otherwise().to("queue:f");    }};CamelContext myCamelContext = new DefaultCamelContext();myCamelContext.addRoutes(builder);</span>


The first line creates an anonymous subclass object of routebuilder, which overwrites the configure () method.
The camelcontext. the builder is called in the addroutes (routerbuilder builder) method. setcontext (this), so the routerbuilder object knows its associated camelcontext object, and then calls builder. configure () method, configure () method body calls such as from (), filter (), choice (), when () isto to (), otherwise () and (). routebuilder. the from (string URI) method calls the getendpoint (URI) of the camelcontext object, searches for the specified endpoint together with the routebuilder object, and then places a frombuilder package to this endpoint. The frombuilder. Filter (predicate) method creates a filterprocessor object for the predicate (condition), which is constructed from the header ("foo"). isprocesto ("bar") statement. In this way, the previous operations incrementally construct a route object and add it to camelcontext.

Important Concepts in camel

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.