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. ArcGIS遥感影像重采样操作及不同算法对比

      本文介绍在ArcMap软件中,实现栅格图像重采样的具体操作,以及不同重采样方法的选择依据.   在之前的博客中,我们介绍了基于Python中Arcpy模块对栅格图像加以批量重采样的方法:而在Arc ...

  2. nginx 访问 nodejs 慢的问题

    1.问题现象 通过nginx 访问 nodejs 访问特别慢.但是通过nodejs 直接访问速度很快响应. 2.日志错误 upstream timed out (10060: A connection ...

  3. 盘点一下在swagger中一些有用且经常忽略的属性

    震惊!,这些Swagger的属性你都了解吗? 盘点一下在swagger中一些有用且经常忽略的属性 启用永久授权EnablePersistAuthorization app.UseSwaggerUI(c ...

  4. 类型判断运算符(as、is、is!)

    类型判断运算符 as.is.is! 运算符是在运行时判断对象类型的运算符. as 类型转换(也用作指定 类前缀)) is 如果对象是指定类型则返回 true is! 如果对象是指定类型则返回 fals ...

  5. PpcProcessResult

    @Data @Accessors(chain = true) public class PpcProcessResult { public static volatile int globalThre ...

  6. CentOS7 安装git 配置秘钥公钥克隆代码

    建议购买阿里云香港服务器可以免备案,系统镜像选择CentOS7测试 第一步:安装git客户端,默认安装在/usr/libexec/git-core目录 yum -y install git #查看版本 ...

  7. Qt编写linux系统onvif工具(支持预览/云台/预置位/录像等)

    一.功能特点 广播搜索设备,支持IPC和NVR,依次返回. 可选择不同的网卡IP进行对应网段设备的搜索. 依次获取Onvif地址.Media地址.Profile文件.Rtsp地址. 可对指定的Prof ...

  8. Qt编写可视化大屏电子看板系统31-模块9视频监控

    一.前言 视频监控模块采用ffmpeg作为解码内核,可以在系统设置中填入一个视频地址,同时支持本地视频文件.USB摄像机.远程onvif摄像机(海康大华等).网络视频地址,大屏系统中默认有一个视频监控 ...

  9. Qt音视频开发39-人脸识别在线版

    一.前言 关于人脸识别这块,前些年不要太火,哪怕是到了今天依然火的一塌糊涂,什么玩意都要跟人脸识别搭个边,这东西应该只是人工智能的一个很小的部分,人脸识别光从字面上理解就是识别出人脸区域,其实背后真正 ...

  10. IM通讯协议专题学习(九):手把手教你如何在iOS上从零使用Protobuf

    本文作者:丁同舟,来自金蝶随手记技术团队. 1.引言 接上篇<金蝶随手记团队的Protobuf应用实践(原理篇)>,本文将以iOS端的Objective-C代码为例,图文并茂地向您菔救绾卧 ...