Apache Camel Series (2)----Hello World

Source: Internet
Author: User
Tags apache camel

Below you create an Apache camel Hello World program that uses Maven,intellij 15 and the running environment is JDK 8. 1. Create a MAVEN project to add Apache Camel dependencies to the Pom.xml file.
<Dependencies>        <Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>3.8.1</version>            <Scope>Test</Scope>        </Dependency>        <Dependency>            <groupId>Org.apache.camel</groupId>            <Artifactid>Camel-core</Artifactid>            <version>2.17.0</version>        </Dependency>        <Dependency>            <groupId>Org.apache.camel</groupId>            <Artifactid>Camel-stream</Artifactid>            <version>2.17.0</version>        </Dependency>    </Dependencies>
2. Create and run camel by creating a new App.java file.
/*** Hello world!*/ Public classApp { Public Static voidMain (string[] args)throwsException {camelcontext context=NewDefaultcamelcontext ();//1. Create a camelcontext.Context.addroutes (NewRoutebuilder () { Public voidConfigure () {from ("timer://foo?fixedrate=true&period=1000"). Process (NewProcessor () { Public voidProcess (Exchange Exchange)throwsException {exchange.getout (). Setbody (NewDate ()); }}). to ("Stream:out");//2. Configure the component or endpoint for the route.            }        }); //3. Add route to CamelcontextContext.settracing (true); Context.start (); //4. Start Camelcontext.Thread.Sleep (Integer.max_value);//in order to keep the camelcontext in working condition, the main thread of sleep is requiredContext.stop ();//Last Stop Camelcontext    }}

The steps to start camel are simple, 1. Create a camelcontext.2. Configures a component or endpoint for a route. 3. Add routes to CAMELCONTEXT4. Start Camelcontext. The above program to achieve the following functions: 1, using the Timer component, send a message every 1s. 2, the message was hijacked by processor before it reached the to, processor added the current time as the message content. 3, use stream component to print the received message to the console. 4, about Exchange, is the object to be exchanged, through Exchange.getin () you can get the Message,exchange.getout () passed from the From, or set the message that will be sent to. The message is also divided into headers and body, similar to the header protocol and protocol content in the HTML protocol.    Headers is a map<string, object>,body can be any Object. 3. Camel can be well integrated with spring, and some components, such as spring-redis-component, are built on spring. Below you create a spring-based Camelhelloworld program. 1, because using spring, you need to add a camel-spring reference to the Pom.xml file and add the following Pom.xml file as follows:
<Dependencies>        <Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>3.8.1</version>            <Scope>Test</Scope>        </Dependency>        <Dependency>            <groupId>Org.apache.camel</groupId>            <Artifactid>Camel-core</Artifactid>            <version>2.17.0</version>        </Dependency>        <Dependency>            <groupId>Org.apache.camel</groupId>            <Artifactid>Camel-spring</Artifactid>            <version>2.17.0</version>        </Dependency>        <Dependency>            <groupId>Org.apache.camel</groupId>            <Artifactid>Camel-stream</Artifactid>            <version>2.17.0</version>        </Dependency>    </Dependencies>
2. Create a spring bean configuration file under the Resources folder.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd Http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd ">        <BeanID= "Myprocess"class= "Com.stepnetwork.test.MyProcossor"></Bean>    <CamelcontextID= "Camel"xmlns= "Http://camel.apache.org/schema/spring">        <Route>            < fromURI= "timer://foo?fixedrate=true&amp;period=1000" />            <Processref= "Myprocess"></Process>            < toURI= "Stream:out" />        </Route>    </Camelcontext></Beans>

The above configuration file defines a camel and adds a route. Com.stepnetwork.test.MyProcossor is defined as follows:
/***/Publicclassimplements  Processor    {  publicvoidthrows  Exception {        exchange.getout (). Setbody (New  Date (). toString ());}    }
3. Create a App2.java file and start spring.
/***/Publicclass  App2    {publicstatic  voidthrows  Exception {        new classpathxmlapplicationcontext ("Context.xml ");        Context.start ();        System.in.read ();    }}

The above two applications will have the following output:
slf4j:failed to load Class "Org.slf4j.impl.StaticLoggerBinder". Slf4j:defaulting to No-operation (NOP) Logger Implementationslf4j:see http://www.slf4j.org/codes.html# Staticloggerbinder for further details. Tue 15:23:34 CST 2016Tue 15:23:35 CST Process finished with exit code 130

Apache Camel Series (2)----Hello World

Related Article

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.