Apache Camel系列(2)----Hello World
来源:互联网 发布:淘宝网懒人桌 编辑:程序博客网 时间:2024/06/08 02:15
下面创建一个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&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 implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.Tue May 10 15:23:34 CST 2016Tue May 10 15:23:35 CST 2016 Process finished with exit code 130
阅读全文
0 0
- Apache Camel系列(2)----Hello World
- Apache Camel系列(4)----Akka Camel
- Android 系列 1.6使用Apache Maven创建“Hello,World”应用程序
- Twisted系列-Hello world.
- Apache Camel系列(1)----使用场景
- Apache Camel系列(3)----Redis组件
- Apache Ignite Hello world ! 配置
- HELLO,WORLD进阶汇编程序系列
- [andorid系列]Hello world入门教程
- luasocket系列: luasocket hello world!
- SpringBoot入门系列:Hello World
- SpringBoot入门系列:Hello World
- Apache Camel
- Apache Camel
- Robotics Studio (MSRS) 入门翻译系列之 2 : Hello World
- Intellij Idea系列之简单Hello World(2)
- SpringMVC自学系列(2)——Hello World
- Tutorial: Hello World with Apache Ant
- hdu 1071 The area
- Jquery Ajax方法传值到action的方法
- android Handler机制 消息机制
- Java线程-5:线程的同步与锁
- Android—RecyclerView带你飞
- Apache Camel系列(2)----Hello World
- 如何将原型设计的优势发挥到淋漓尽致?
- (UVA
- python3--str的使用1
- mac下配置python的虚拟环境virtualenv和虚拟环境管理包virtualenvwrapper
- Android 快速发布开源项目到jcenter
- session和cookie的区别
- IDEA使用Tomcat和Maven插件启动maven项目的参数配置;
- Java线程-6:线程的交互