下面创建一个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

Apache Camel系列(2)----Hello World的更多相关文章

  1. Apache Shiro系列之五,概述 —— 配置

    Shiro设计的初衷就是可以运行于任何环境:无论是简单的命令行应用程序还是复杂的企业集群应用.由于运行环境的多样性,所以有多种配置机制可用于配置,本节我们将介绍Shiro内核支持的这几种配置机制.   ...

  2. Apache Shiro系列四,概述 —— Shiro的架构

    Shiro的设计目标就是让应用程序的安全管理更简单.更直观.     软件系统一般是基于用户故事来做设计.也就是我们会基于一个客户如何与这个软件系统交互来设计用户界面和服务接口.比如,你可能会说:“如 ...

  3. Apache Shiro系列三,概述 —— 10分钟入门

     一.介绍 看完这个10分钟入门之后,你就知道如何在你的应用程序中引入和使用Shiro.以后你再在自己的应用程序中使用Shiro,也应该可以在10分钟内搞定. 二.概述 关于Shiro的废话就不多说了 ...

  4. Apache Camel

    Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...

  5. Apache Camel之FTP组件学习

    写在最前面 哎,最近提了离职,手头的活也基本上清理的差不多了.想着这个把月可以舒服的晃悠晃悠的离开,但是运维的小伙伴总是不架势,走之前还是提了个新需求. 先说下需求吧,我们的系统概括的讲就是一个接口系 ...

  6. spring boot + apache camel 传输文件

    一 sftp搭建略 这里简单说一下为什么使用sftp.ftp和sftp各有优点,差别并不是太大.sftp安全性好,性能比ftp低.ftp对于java来说并不复杂,效率也高.之所以使用sftp主要是可以 ...

  7. apache kafka系列之Producer处理逻辑

     最近研究producer的负载均衡策略,,,,我在librdkafka里边用代码实现了partition 值的轮询方法,,,但是在现场验证时,他的负载均衡不起作用,,,所以来找找原因: 下文是一篇描 ...

  8. [每日一学]apache camel简介

    apache camel 是轻量级esb框架.如下是它的架构图: 它有几个比较重要的概念就是: 1.endpoint,所谓的endpoint,就是一种可以接收或发送数据的组件.可以支持多种协议,如jm ...

  9. [每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock

    开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的. 现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用 ...

  10. Apache Camel继承Spring Boot 实现文件远程复制和转移

    pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-f ...

随机推荐

  1. Python:pygame游戏编程之旅四(游戏界面文字处理)

    本节讲解游戏界面中字体的处理,以在界面中实时显示当前时间.小球位置为例进行实验,具体见代码. 一.代码 # -*- coding:utf-8 -*- import os import sys impo ...

  2. Kafka可视化工具之Kafka Tool

    官网: https://www.kafkatool.com/download.html Kafka Tool是一个用于管理和使用Apache Kafka集群的GUI应用程序. Kafka Tool提供 ...

  3. Graylog之进阶操作

    有关系统的索引,权限,pipline 参考文章: https://mp.weixin.qq.com/s/LROHPjZEJPMsS5M_a3pPFw https://blog.csdn.net/wei ...

  4. java 死锁问题排查

    排查过程 1.识别死锁现象 通常,死锁会表现为应用程序挂起,不响应用户请求或 cpu 使用率下降. 2.收集线程转储 当应用出现不响应时,可以使用以下方法收集线程转储: jstack -l 进程ID ...

  5. Node.js 文件读写

    1.fs模块 在node.js中,所有文件的操作都是通过fs模块来实现的.包括文件目录的创建,删除,查询以及文件的读取,写入. 在fs模块中,所有的方法都分成同步和异步两种实现,具有sync后缀的为同 ...

  6. 基于云主机的ModelArts模型训练实践,让开发环境化繁为简

    本文分享自华为云社区<[开发者空间实践]云主机安装Docker并制作自定义镜像在ModelArts平台做模型训练>,作者: 开发者空间小蜜蜂. 1.1 案例介绍 在AI业务开发以及运行的过 ...

  7. PL/SQL中文乱码修正

    我根据需求,,需要修改 数据库的部分表格的部分字段,然而在Update的时候,出现了中文乱码(Type字段). 此时,我用的是客户端,服务器没有安装,在另一台机器上,所以,我需要做的是修改客户端编码: ...

  8. 规模法则(Scaling Law)与参数效率的提高,

    上一篇:<人工智能大语言模型起源篇(三),模型规模与参数效率> 规模法则与效率提高 如果你想了解更多关于提高变换器效率的各种技术,我推荐阅读2020年的<Efficient Tran ...

  9. MeteoInfo-Java解析与绘图教程(十)_JAVA绘制雷达PPI图

    天气雷达的基本要素有很多,特别是双偏振雷达更多,但业务场景经常使用的一般为基本反射率,基本速度这两种要素 接下来我们以基本反射率为例,其他的要素也是一样的,一通百通 首先我们做基本反射率的图需要确定做 ...

  10. vue3笔记

    如何创建vue3项目 基于 vue 脚手架 npm i @vue/cli -g vue create <project-name> cd <project-name> npm ...