Apache camel framework integrated with spring

Source: Internet
Author: User
Tags apache camel

Apache camel provides integration with spring and manages camelcontext through the spring container (applicationcontext). In this way, you do not need to write code to control camelcontext initialization, start and stop. camel will be started with spring startup.

This article integrates the examples in the Apache camel framework getting started example (http://blog.csdn.net/kkdelta/article/details/7231640) into spring, the following describes the basic steps of integration.

1. Create an Eclipse project and configure the jar packages of spring3 and camel to classpath of the project.

2. The route class inherits the routebuilde, as shown below:

public class FileProcessWithCamelSpring extends RouteBuilder {    @Override    public void configure() throws Exception {        FileConvertProcessor processor = new FileConvertProcessor();         from("file:d:/temp/inbox?delay=30000").process(processor).to("file:d:/temp/outbox");            }}

3. processor is still the same as the sample code for getting started.

public class FileConvertProcessor implements Processor{    @Override    public void process(Exchange exchange) throws Exception {            try {            InputStream body = exchange.getIn().getBody(InputStream.class);            BufferedReader in = new BufferedReader(new InputStreamReader(body));            StringBuffer strbf = new StringBuffer("");            String str = null;            str = in.readLine();            while (str != null) {                                System.out.println(str);                strbf.append(str + " ");                str = in.readLine();                            }            exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");            // set the output to the file            exchange.getOut().setBody(strbf.toString());        } catch (IOException e) {            e.printStackTrace();        }    }}

4. Create a spring configuration file as follows: note that you must add the xmlns of camel to the file.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:camel="http://camel.apache.org/schema/spring"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"    default-autowire="byName"  default-init-method="init">    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">        <package>com.test.camel</package>    </camelContext>    </beans>

5. Start the spring container. Camel will be automatically started. You do not need to use camelcontext context = new defaultcamelcontext (), context. addroutes (...); context. Start ();

Applicationcontext AC = new classpathxmlapplicationcontext ("config/cameltest. xml ");
While (true ){
Thread. Sleep (2000 );
}

Obviously, camel can be easily integrated with spring.

Camel also provides "Spring DSL" to configure route rules in XML, and does not need to use Java classes (such as fileprocesswithcamelspring) to implement route.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:camel="http://camel.apache.org/schema/spring"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"    default-autowire="byName"  default-init-method="init">    <bean id="fileConverter" class="com.test.camel.FileConvertProcessor"/>    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">        <route>            <from uri="file:d:/temp/inbox?delay=30000"/>            <process ref="fileConverter"/>            <to uri="file:d:/temp/outbox"/>        </route>    </camelContext></beans>

Start the spring container as in step 5. Camel polls every 30 seconds to check whether D:/temp/inbox has files. If yes, process the logs.

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.