Apache Camel系列(2)----Hello World


下面創建一個Apache Camel的Hello World程序,該程序使用Maven,Intellij 15,運行環境是JDK 8。
 
1,創建一個maven工程,在pom.xml文件中添加apache camel的dependencies。
 
複製代碼
<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. 新建App.java文件,創建並運行camel。
 
複製代碼
/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 創建 CamelContext.
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("timer://foo?fixedRate=true&period=1000").
                        process(new Processor() {
                            public void process(Exchange exchange) throws Exception {
                                exchange.getOut().setBody(new Date());
                            }
                        }).to("stream:out"); // 2. 爲路由配置組件或終端節點.
            }
        }); // 3. 添加路由到CamelContext
        context.setTracing(true);
        context.start(); // 4. 啓動CamelContext.
        Thread.sleep(Integer.MAX_VALUE);  // 爲了保持CamelContext處於工作狀態,這裏需要sleep主線程
        context.stop(); // 最後停止CamelContext
    }
}
複製代碼

 

啓動camel的步驟很簡單,
 
1. 創建 CamelContext.
2. 爲路由配置組件或終端節點.
3. 添加路由到CamelContext
4. 啓動CamelContext.
 
以上程序實現如下功能:
1,使用timer組件,每隔1s發送一次消息.
2,消息在到達to之前,被Processor劫持,Processor添加了當前時間作爲消息內容。
3,使用stream component,將接收到的消息打印到控制檯。
4,關於exchange,是用來交換的對象,通過exchange.getIn()可以獲取從from傳遞過來的Message,exchange.getOut()或以設置將要發給to的Message。
Message又分爲headers和body,類似於html協議中的頭協議和協議內容。headers是一個Map<String, Object>,body可以是任意Object。
 
 
 
 
3. Camel可以很好的和Spring集成,一些組件如spring-redis-component,就是基於Spring構建的。
 
下面創建基於Spring的CamelHelloWorld程序。
 
1,因爲使用Spring,需要在pom.xml文件中添加camel-spring引用,添加之後的pom.xml文件如下:
 
複製代碼
<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,在資源文件夾下創建Spring bean配置文件。
 
複製代碼
<?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.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    
    <bean id="myProcess" class="com.stepnetwork.test.MyProcossor"></bean>

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="timer://foo?fixedRate=true&amp;period=1000" />
            <process ref="myProcess"></process>
            <to uri="stream:out" />
        </route>
    </camelContext>
</beans>
複製代碼

 

以上配置文件定義了一個camel,以及添加了route。com.stepnetwork.test.MyProcossor的定義如下:
 
複製代碼
/**
 * Created by sam on 5/9/16.
 */
public class MyProcossor implements Processor {
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody(new Date().toString());
    }
}
複製代碼
 
3, 創建App2.java文件,啓動Spring。
 
複製代碼
/**
 * Created by sam on 5/10/16.
 */
public class App2 {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        context.start();
        System.in.read();
    }
}
複製代碼

 

以上兩個應用程序都能得到如下輸出:
 
複製代碼
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Tue May 10 15:23:34 CST 2016
Tue May 10 15:23:35 CST 2016
 
Process finished with exit code 130
複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章