Redis组件允许你从Redis接收消息,以及将消息发送给Redis。RedisProducer的功能很强大,几乎能执行所有的Redis Command,这些Command都是在Message的header中进行设置的。遗憾的是RedisConsumer仅仅支持pub/sub模式,不支持Point2Point,这意味这在Camel中,通过阻塞的方式消费Lists中的消息是不可行的。我反馈了这个问题到Apache Camel Mail List,希望以后的版本支持P2P更能。下面演示如何使用camel-spring-redis组件。
 
使用Spring
1,创建Maven工程,添加camel-spring-redis引用,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-spring-redis</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="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="9999" />
<property name="password" value="1234567890" />
</bean>
<bean id="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route startupOrder="1">
<from uri="timer://foo?fixedRate=true&amp;period=1000"/>
<setHeader headerName="CamelRedis.Command">
<constant>SET</constant>
</setHeader>
<setHeader headerName="CamelRedis.Key">
<constant>keyOne</constant>
</setHeader>
<setHeader headerName="CamelRedis.Value">
<constant>valueOne</constant>
</setHeader>
<to uri="spring-redis://localhost:9999?connectionFactory=#connectionFactory&amp;serializer=#serializer"/>
</route>
</camelContext>
</beans>
上边的beans文件中,定义了bean connectionFactory,可以设置redis服务器的相关信息,比如密码。如果你的redis服务器没有设置密码,那么这个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="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route startupOrder="1">
<from uri="timer://foo?fixedRate=true&amp;period=1000"/>
<setHeader headerName="CamelRedis.Command">
<constant>SET</constant>
</setHeader>
<setHeader headerName="CamelRedis.Key">
<constant>keyOne</constant>
</setHeader>
<setHeader headerName="CamelRedis.Value">
<constant>valueOne</constant>
</setHeader>
<to uri="spring-redis://localhost:9999?serializer=#serializer"/>
</route>
</camelContext>
</beans>
setHeader中设置了Redis的相关命令,RedisProducer支持几乎所有的RedisCmmand,具体可参见Redis Component官网
这里的意思是每隔1S为Redis服务器的keyOnve设置一个值valueOne。
 
3, 创建App3.java,启动spring。

/**
* Created by sam on 5/10/16.
*/
public class App3 {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
context.start();
System.in.read();
}
}
启动ClassPathXmlApplicationContext的时候,系统会自动创建并运行CamelContext。
 
监控Redis,得到结果如下:
 
[sam@localhost redis-2.8.19]$ src/redis-cli -p 9999 -a 1234567890
127.0.0.1:9999> MONITOR
OK
1462931627.929228 [0 127.0.0.1:50939] "PING"
1462931686.110952 [0 127.0.0.1:50943] "AUTH" "1234567890"
1462931686.120240 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
1462931687.065705 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
1462931688.066442 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
1462931689.066169 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
1462931690.065948 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
1462931691.065674 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
不使用Spring
公司不是所有的项目都用了Spring,所以研究了两天,做了个非Spring的Demo,还是maven工程,pom.xml配置文件和上边的一样,创建app4.java类,代码如下:
 
 
public class App4 {
public static void main(String[] args) throws Exception {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); // 创建connectionFactory
connectionFactory.setHostName("localhost");
connectionFactory.setPassword("1234567890");
connectionFactory.setPort(9999);
SimpleRegistry registry = new SimpleRegistry();
connectionFactory.afterPropertiesSet(); // 必须要调用该方法来初始化connectionFactory
registry.put("connectionFactory", connectionFactory); //注册connectionFactory
registry.put("serializer", new StringRedisSerializer()); //注册serializer CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("stream:out"));
from("timer://foo?fixedRate=true&period=1000").
setHeader("CamelRedis.Command", constant("PUBLISH")).
setHeader("CamelRedis.Channel", constant("testChannel")).
setHeader("CamelRedis.Message", constant(new Date().toString())).
to("spring-redis://localhost:9999?connectionFactory=#connectionFactory&serializer=#serializer");
}
});
context.setTracing(true);
context.start();
Thread.sleep(Integer.MAX_VALUE);
context.stop();
}
}
这段代码主要是使用SimpleRegistry来注册bean信息,并将SimpleRegistry作为CamelContext的参数,这样在endpoint的Url中就可以使用之前注册的bean了。
另外在创建完connectionFactory后要调用afterPropertiesSet()方法来完成初始化。如果你的redis没有设置密码,并且不需要serializer,那么代码更简单,如下:
 
public class App4 {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
errorHandler(deadLetterChannel("stream:out"));
from("timer://foo?fixedRate=true&period=1000").
setHeader("CamelRedis.Command", constant("PUBLISH")).
setHeader("CamelRedis.Channel", constant("testChannel")).
setHeader("CamelRedis.Message", constant(new Date().toString())).
to("spring-redis://localhost:9999");
}
});
context.setTracing(true);
context.start();
Thread.sleep(Integer.MAX_VALUE);
context.stop();
}
}
以上代码经测试,都可运行。

Apache Camel系列(3)----Redis组件的更多相关文章

  1. Apache Camel之FTP组件学习

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

  2. spring boot + apache camel 传输文件

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

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

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

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

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

  5. 在Apache Tomcat 7设置redis作为session store

    在Apache Tomcat 7设置redis作为session store  //输出tomcat控制台日志 root@ubuntu:~# cd /usr/tomcat/apache-tomcat- ...

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

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

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

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

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

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

  9. Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件 (转)

    1.概要: 本项目主要是通过在Spring平台上配置Camel.FTP,实现定时从FTP服务器下载文件到本地.解析文件.存入数据库等功能. 2.搭建空项目: Spring Boot有几种自动生成空项目 ...

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

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

随机推荐

  1. Javascript遍历目录时使用for..in循环无法获取Files对象和SubFolders对象问题的解决方法

    1 Javascript遍历目录时使用for..in循环无法获取Files对象和SubFolders对象 1.1 问题场景   在JavaScript中遍历目录,使用for.. in循环时,无法获取到 ...

  2. canvas(八)绘制图片和坐标转换

    1.绘制图片 相关api及其参数:ctx.drawImage() 参数 说明 参数一 图片对象 参数二,三 可选,图片裁剪的基点(原图左上角为原点) 参数四,五 可选,图片裁剪区域的宽高(基于原图大小 ...

  3. std的map或者set中,比较浮点类型二维三维数据

    在map和set中,如果比较对象是二维或者三维数据,需要把二维三维数据的浮点数转换为比较精度. 如果比较精度是0.001,那么数据的精度也必须是0.001,不然会出现如下情况: 比较函数 struct ...

  4. 10C++选择结构(4)

    一.switch语句 问题:风之巅小学规定,若测试成绩大于或等于90分为"A",大于或等于70分小于90分为"B",大于或等于60分小于70分为"C& ...

  5. JEP 462 结构化并发是一个很愚蠢的提案

    https://openjdk.org/jeps/462 Motivation Developers manage complexity by breaking tasks down into mul ...

  6. 【Linux】Deepin安装Anaconda后手动配置环境变量

    配置环境变量 如果安装完毕后,重新打开终端,输入conda表示没找到这个命令 说明没有配置环境变量 那么我们首先第一件事,就是找到我们的conda安装到哪里了 例如我的是安装到主目录的(默认在这的,安 ...

  7. debian/ubuntu系统vi无法删除字符的解决办法

    之前在 Linux 下操作,一直使用的是 Centos 系统,使用 vi 编辑命令一直很顺畅. 最近,入手了一台 debian 操作系统的 vps.在操作 vi 命令时,发现当输入 i 要进行文件编辑 ...

  8. 管理员应了解的 SIEM解决方案七大功能 !

    ​SIEM解决方案已成为企业网络安全武器库中不可或缺的一部分.但由于SIEM功能过于复杂且架构难以理解,企业往往SIEM的潜在功能.遗憾的是,他们忽视的潜在功能正是解开企业网络合规的重要部分. 例如, ...

  9. Qt编写的项目作品31-PDF阅读器(雨田哥作品)

    一.功能特点 仿WPS界面. 预览PDF文件. 支持PDF预览放大.缩小. 支持目录预览查看. 支持目录点击跳转页查看. 支持页数指定跳转. 支持上一页.下一页.首页.尾页跳转. 支持鼠标拖拽滑动预览 ...

  10. [转]CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options

    这个错误是使用webpack的一个plugin出现的错误.这个plugin是copy-webpack-plugin我把文档的例子复制,然后就报了这个错误.文档的例子: const CopyPlugin ...