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.xsdhttp://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.xsdhttp://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(); // 创建connectionFactoryconnectionFactory.setHostName("localhost");connectionFactory.setPassword("1234567890");connectionFactory.setPort(9999);SimpleRegistry registry = new SimpleRegistry();        connectionFactory.afterPropertiesSet(); // 必须要调用该方法来初始化connectionFactoryregistry.put("connectionFactory", connectionFactory); //注册connectionFactoryregistry.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();}
}

以上代码经测试,都可运行。

转载于:https://www.cnblogs.com/zengbiaobiao2016/p/5481029.html

Apache Camel系列(3)----Redis组件相关推荐

  1. java camel,java – 返回基础知识:Apache Camel路由和直接组件

    我对Camel路线及其两个端点感到困惑:Direct和Seda.好吧,假设我有这样的路线: public void configure() { from("direct:services&q ...

  2. camel seda 协议_探索Apache Camel Core – Seda组件

    camel seda 协议 Apache Camel中的seda组件与我在先前的博客中介绍的direct组件非常相似,但是以异步的方式. 为此,它使用java.util.concurrent.Bloc ...

  3. 探索Apache Camel Core – Seda组件

    Apache Camel中的seda组件与我在之前的博客中介绍的direct组件非常相似,但是以异步的方式. 为此,它使用java.util.concurrent.BlockingQueue作为默认实 ...

  4. 架构设计:系统间通信(36)——Apache Camel快速入门(上)

    1.本专题主旨 1-1.关于技术组件 在这个专题中,我们介绍了相当数量技术组件:Flume.Kafka.ActiveMQ.Rabbitmq.Zookeeper.Thrift .Netty.DUBBO等 ...

  5. Apache Camel源码研究之Rest

    本文以Camel2.24.3 + SpringBoot2.x 为基础简单解读Camel中的Rest组件的源码级实现逻辑. 0. 目录 1. 前言 2. 源码解读 2.1 启动时 2.1.1 `Rest ...

  6. apache camel 相关配置_Apache Camel Spring Boot

    Camel应用初始化 Apache Camel 采用的是组件化的设计思想,通过Camel Component对接第三方的应用,Camel核心模块会扫描classpath 加载这些Camel Compo ...

  7. apache camel_探索Apache Camel Core –文件组件

    apache camel 文件轮询器是解决常见IT问题的非常有用的机制. Camel的内置file组件非常灵活,并且有许多选项可用于配置. 让我们在这里介绍一些常用用法. 轮询目录以输入文件 这是一条 ...

  8. Apache Camel日志组件示例

    Apache Camel日志组件示例 您要将消息记录到底层的记录机制,请使用骆驼的log:组件. Camel使用sfl4j作为记录器API,然后允许您配置记录器实现. 在本文中,我们将使用Log4j作 ...

  9. 探索Apache Camel Core –文件组件

    文件轮询器是解决常见IT问题的非常有用的机制. Camel的内置file组件非常灵活,并且有许多选项可用于配置. 让我们在这里介绍一些常用用法. 轮询输入文件的目录 这是典型的骆驼Route用于每秒轮 ...

  10. 适用于ActiveMQ 5.9的Apache Camel Broker组件

    将Apache Camel嵌入ActiveMQ代理可以为使用Camel的集成功能扩展消息代理提供极大的灵活性. Apache Camel路由的另一个好处是,如果使用activemq组件 ,则可以避免远 ...

最新文章

  1. java源码-AQS机制
  2. Fedora 31 Beta 准时发布,带来许多激动人心的更新
  3. 第一天2017/03/28
  4. 给你汇报Struts2 S2-016漏洞修复的总结
  5. 学习asp.net ajax 笔记(一)
  6. 《UCD火花集2:有效的互联网产品设计 交互/信息设计 用户研究讨论》一2.3 交互设计师容易犯的错误:把自己禁锢在解决方案之中...
  7. Netty(二)(入门篇)传统的Bio编程
  8. php mysql 地图 矩形_PHP+Mysql+jQuery中国地图区域数据统计实例讲解
  9. 使用Fluent NHibernate和AngularJS的Master Chef(第1部分)ASP.NET Core MVC
  10. Apache+php 在windows下的配置
  11. Bailian4121 股票买卖【最值】
  12. Apache+tomcat集群
  13. python字典有序还是无序_python--基础语法
  14. Web基础(Java前端基础)
  15. python蒙特卡洛模拟_用Python实现蒙特卡洛模拟
  16. 完美解决织梦CMS加入lian666自动友情链接代码正常显示
  17. Nginx基础之错误页面配置/流量控制/访问控制/变量/监控/HTTPS配置/性能优化
  18. 18650圆柱锂电池comsol5.6模型 参数已配置,电化学生热研究,三种放电倍率,
  19. 信息数据采集软件-什么工具可以快速收集信息
  20. Java实现对文件的增删改查操作

热门文章

  1. Java fluent风格
  2. JBOSS EAP6.2.0的下载安装、环境变量配置以及部署
  3. 极限运动:街头极限单车,不只是牛逼!
  4. 构建samba文件共享服务器
  5. Scanner初学需要注意的几个问题
  6. docker portainer_Docker可视化管理:Portainer中文版
  7. binder.java 565_Android跨进程抛异常的原理的实现
  8. zookeeper中的ZAB协议理解
  9. 【渝粤教育】国家开放大学2018年秋季 0553-22T色彩 参考试题
  10. 【渝粤教育】国家开放大学2018年春季 3923T汽车维修企业管理 参考试题