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
复制代码

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: Camel 实战 原名: Camel in Action 作者: Claus Ibsen Jonathan Anstey 资源格式: PDF 版本: 英文文字版/更新源代码 出版社: Manning书号: 9781935182368发行时间: 2010年12月 地区: 美国 语言: 英文 简介: 内容介绍: Apache Camel is a Java framework that lets you implement the standard enterprise integration patterns in a few lines of code. With a concise but sophisticated DSL you snap integration logic into your app, Lego-style, using Java, XML, or Scala. Camel supports over 80 common transports such as HTTP, REST, JMS, and Web Services. Camel in Action is a Camel tutorial full of small examples showing how to work with the integration patterns. It starts with core concepts like sending, receiving, routing, and transforming data. It then shows you the entire lifecycle and goes in depth on how to test, deal with errors, scale, deploy, and even monitor your app—details you can find only in the Camel code itself. Written by the developers of Camel, this book distills their experience and practical insights so that you can tackle integration tasks like a pro. 目录: Part 1 First steps 1 Meeting Camel 2 Routing with Camel Part 2 Core Camel 3 Transforming data with Camel 4 Using beans with Camel 5 Error handling 6 Testing with Camel 7 Understanding components 8 Enterprise integration patterns Part 3 Out in the wild 9 Using transactions 10 Concurrency and scalability 11 Developing Camel projects 12 Management and monitoring 13 Running and deploying Camel 14 Bean routing and remoting appendix A Simple, the expression language appendix B Expressions and predicates appendix C The producer and consumer templates appendix D The Camel community appendix E Akka and Camel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值