Open-source Apache camel simplifies the SOA implementation process (now released in camel2.7)

Source: Internet
Author: User
Tags apache camel xquery

In the past few years, integrated technology has developed by leaps and bounds. The XML/rest/Web Service/SOA revolution constantly encourages engineers and software companies to create a wide range of protocols, adapters, transmitters, containers, and standards, best practices · everything.

It is undeniable that the existing code is very complex and diversified, and there is almost nothing they can do. However, these software packages are built from the technical perspective, which makes it a great challenge for readers to effectively use their functions.

At present, many readers have completed this challenge. The rich experience and thousands of successful cases have promoted the formation of many definitions of the basic architecture design patterns, so as to help developers get started with integration and avoid detours. Among them, there is a set of design patterns that have attracted attention in the industry, that is, the hohpe And Woolf's enterprise integration models. These patterns contain some technically unrelated words to describe a large number of integrated solutions. Rather than focusing on low-level programming, they adopt top-down solutions to develop an asynchronous, information-based architecture.

A consistent vocabulary is good. Isn't it better to build an easy-to-use framework when actually building the infrastructure?

This is exactly the idea behind Apache's open-source camel project. Now, an effective and realistic set of modes is available. Obviously, the next step is to create a drive that can implement this mode in the simplest way.

Camel is a code-first tool that allows developers to perform precise large-scale integration without learning vendor customization and complex underlying technologies. Camel uses the descriptive private language of the Java field to connect information systems and configure routing and Mediation Rules, and implements the enterprise integration mode based on pojo. In this way, developers do not have to read technical specifications such as JMS or jbi one page after another, you can directly design and establish a Service-Oriented Architecture (SOA) without having to deal with lower-level spring frameworks ).

Apache camel is derived from the code and inspiration of other Apache projects, especially Apache activemq and Apache ServiceMix. Members of the Project discovered that people want to establish or use the patterns in the Enterprise Integration Model Book in many different situations. Therefore, the camel team began to establish such a framework with such a clear purpose.

Camel Overview:

The first step to establish camel is to solve the patterns in the underlying framework. Some people want to use the mode in the Enterprise Service Bus (ESB), while others choose the mode in Message Broker, while others need to use the mode of the application or communicate with information providers. Some people still want to use it in the Web service framework or other communication platforms. Compared to binding this routing code to a specific message broker or Enterprise Service Bus (ESB), camel chooses to extract this code as an independent framework, in this way, it can be applied to any project. Camel has a script-whether in servlet or web service package, full Enterprise Service Bus (ESB) or information application can be reused anywhere.

Camel's initial and major advantage was that the development team did not have to work on containers to connect the system. Many people think that working hard on containers is a correct way or even a test of courage, but these obstacles have become unnecessary obstacles for more and more teams. With Apache camel, developers can minimize irrelevant tasks during task completion. If other requirements are ensured, camel can also be implemented in the jbi container, but this is not necessary.

To simplify programming, camel also supports specific Java and XML domain languages, so that you can use the enterprise integration mode in all Java ide or spring XML. This higher level of extraction can solve problems more effectively.

Camel reuses many spring 2 features, such as declarative transactions, configuration inversion, and many practical aspects to work with JMS and JDBC, Java persistence API (JPA. This improves the extraction level, simplifies programming, and reduces the XML that needs to be written. However, if people make some effort at the bottom, they will also find that camel still exposes line-level access.

Camel example

We will explain different ways to implement Apache camel configuration. First, we will use Java DSL (domain-specific language) and then spring xml configuration.

Java DSL Configuration

The following column is implemented when you want to get information from the JMS pair column in a directory structure. First, we need to create a camelcontext object:

Camelcontext context = new defaultcamelcontext ();

There are more than one way to add components to camelcontext. You can also add components after creating a route, as shown in the following example:

Context. addroutes (New routebuilder (){
Public void configure (){
From ("test-JMS: queue: Test. queue"). To ("file: // test ");
// Set up a listener on the file component
From ("file: // test"). Process (new processor (){
Public void process (exchange e ){
System. Out. println ("stored ed exchange:" + E. getin ());
}
});
}
});

Or explicitly add the JMS component as follows:

Connectionfactory = new activemqconnectionfactory ("VM: // localhost? Broker. Persistent = false ");
// Note we can explicity name the component
Context. addcomponent ("test-JMS", jmscomponent. jmscomponentautoacknowledge (connectionfactory ));

Next time, you must start camel context. If you use spring to configure camel context, this will automate the process for you. If you are using pure Java, you must first call the START () method:

Camelcontext. Start ();

This will start all the configuration routing rules.

After we fully start camelcontext, We can input some objects to camel.

Generally, some functional components are used to directly input some information or events to camel, but here we will use cameltemplate, this is also the simplest way to test whether the previous configuration is correct.

Cameltemplate template = new cameltemplate (context );

In this case, we can use cameltemplate to send some test messages through JMS:

For (INT I = 0; I <10; I ++ ){
Template. sendbody ("test-JMS: queue: Test. queue", "Test message:" + I );
}

We include an object (usually a Text object) from cameltemplate into camelcontext and provide it to the test component JMS: queue: Test. queue. These text objects are automatically modified to the JMS information and posted as test. queue in the JMS peer column. When we create a route, we configure filecomponent to receive test. queue.

The filecomponent file extracts information from the column and stores it in a directory named test. Each piece of information is stored in a file corresponding to its destination or information Id.

Finally, we configure our own listener and get the notification from filecomponent, which is printed in text format.

Spring xml configuration

In this example, the spring xml configuration is used to convert the file and the result is sent to the JMS peer column from the directory using XQuery. It selects a file from the directory for parsing, converts it using XQuery, and sends it to the Message column. Of course, to better understand the generated file, we also have some other methods to select content from the JMS column and write it to an output directory.

Running example

Let's use the camel Maven plug-in to run this example. For example, you can perform the following operations through the source code or executable edition of open-source software:

CD examples/camel-Example-spring-XQuery
Mvn camel: Run

Now you can see the file content generated under the target/outputfiles Directory, which is the conversion information read from the JMS column.

Code Lookup

What needs to be done is to start the spring application normally with the META-INF/spring/camelcontext. xml file defined under the class. This is the standard spring XML document for configuring camelcontext using the xml configuration of camel.

Note that at the bottom of the XML sample file, the activemq component and details about how to connect to the proxy are clearly configured.

The key spring XML Code is as follows:

<Camelcontext usejmx = "true" xmlns = "http://activemq.apache.org/camel/schema/spring">

<! -- Lets Parse Files, transform them with XQuery and send them to JMS -->
<Route>
<From uri = "file: src/data? Noop = true "/>
<To uri = "XQuery: mytransform. XQuery"/>
<To uri = "JMS: myqueue"/>
</Route>

<! -- Now lets write messages from the queue to a directory -->
<Route>
<From uri = "JMS: myqueue"/>
<To uri = "file: Target/outputfiles"/>
</Route>

</Camelcontext>

<! -- Lets configure the default activemq broker URL -->
<Bean id = "JMS" class = "org. Apache. Camel. component. JMS. jmscomponent">
<Property name = "connectionfactory">
<Bean class = "org. Apache. activemq. activemqconnectionfactory">
<Property name = "brokerurl" value = "VM: // localhost? Broker. Persistent = false "/>
</Bean>
</Property>
</Bean>

I hope this article will make you realize how practical it is to use Apache camel to achieve enterprise application integration.

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.