Apache Camel Series (1)----usage scenarios

Source: Internet
Author: User
Tags rabbitmq apache camel

Apache Camel is an open-source framework based on enterprise integration pattern, or EIP. The EIP defines a number of message transmission models between different application systems, including common point-to-point,pub/sub models. For more information about EIP, see here Apache Camel mainly provides the following functions: 1, the implementation of most of the EIP mode, if you want to transfer messages between different application systems in different ways, then you can find a solution from Apache camel. 2, provides a large number of component (components), each component is a message middleware (or message service) specific implementation, each message middleware uses different protocols, so you can use a number of different protocols to complete the message transmission. 3, allows the user to define flexible routing rules, from this point of view, Apache camel when a rule engine. So the Apache Camel application scenario has those, here are some: 1, message aggregation, such as you have a message from different servers, there are activemq,rabbitmq,webservice, you want to store them in the log file, you can define the following rules.
1 NewRoutebuilder () {2 @Override3      Public voidConfigure ()throwsException {4From ("Amqp:queue:incoming"). to ("Log:com.mycompany.log?level=debug"));5From ("Rabbitmq://localhost/a/routingkey=b"). to ("Log:com.mycompany.log?level=debug"));6From ("Jetty:http://localhost:8080/myapp/myservice"). to ("Log:com.mycompany.log?level=debug"));7     }8}

from indicates that the message is taken from this endpoing to indicate that the message is sent to this endpoint,endpoint is the message address, which contains the protocol type and URL.

2, message distribution, divided into two kinds, sequential distribution and parallel distribution. In sequential distribution, the message is first endpoing, and the first endpoint processing is completed before being distributed to the next endpoint. If the first endpoing processing fails, the message is not propagated to the second endpoint. For example, there are the following rules: from ("Amqp:queue:order"). to ("Uri:validatebean", "Uri:handlebean", "Uri:emailbean"); The rule is to take order information from the order queue, It then validates the order, processes the order, and sends an email notifying the user. Any one step goes wrong and the next step will not be executed back. Parallel distribution is to send the resulting messages simultaneously to different endpoint, with no sequencing points, and each endpoint processing message is also independent. If you will change from the road to the From ("Amqp:queue:order"). Multicast ("Uri:validatebean", "Uri:handlebean", "Uri:emailbean"). Then the message will be sent to the corresponding endpoint. 3, message conversions, such as the ability to convert XML data to JSON data, can use the following rules.
From ("Amqp:queue:order"). Process (new xmltojsonprocessor ()). to ("Bean:orderhandler");
Where Xmltojsonprocessor is a custom class that inherits Org.apache.camel.Processor and is used to convert XML data into JSON. 4, the Rules engine, you can use the spring Xml, groovy such a DSL to define the route, so that without modifying the code, you can achieve the purpose of modifying business logic. For example, the above rule, from ("Amqp:queue:order"). to ("Uri:validatebean", "Uri:handlebean", "Uri:emailbean"), using the spring XML definition as follows:
<Route>        < fromURI= "Amqp:queue:order"/>        <Multicast>            < toURI= "Uri:validatebean"/>            < toURI= "Uri:handlebean"/>            < toURI= "Uri:emailbean"/>        </Multicast></Route>
If you need to add a log after processing the order, you can rename the following rule
<Route>        < fromURI= "Amqp:queue:order"/>        <Multicast>            < toURI= "Uri:validatebean"/>            < toURI= "Uri:handlebean"/>            < toURI= "Log:com.mycompany.log?level=info"/>            < toURI= "Uri:emailbean"/>        </Multicast></Route>

In addition, Camel provides a large number of built-in processor for logic operations, filtering, etc., which makes it easier to remove flexible route, for example:
From ("Amqp:queue:order"). Filter (Header ("foo"). Isequalto ("bar")). Choice ()    . When (XPath ("/person/ City = & #39; london& #39; " )         . to ("file:target/messages/uk")    . otherwise (). To        ("file:target/messages/ Others ");

This rule first filters the order, processing only the order of Foo in the header, and then passes the order to the different endpoint according to the user's city. There are many applications for Apache Camel, but here are just a few examples. If you are not very understanding camel's application scenario, you can first go to Enterprise integration pattern (business integration mode, referred to as EIP) to find the corresponding scenario, click on each scenario under the link, You can find the corresponding implementation of camel.

Apache Camel Series (1)----usage scenarios

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.