下面创建一个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 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
转自:https://www.cnblogs.com/zengbiaobiao2016/p/5481008.html