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. 使用JConsole监控进程、线程、内存、cpu、类情况

    Jconsole简介: Jconsole是一个JMX兼容的监视工具.它使用Java虚拟机的JMX机制来提供运行在Java平台的应用程序的性能与资源耗费信息. 监控进程使用方法如下:由于JConsole ...

  2. 超详细 HarmonyOS 开发教程之开发环境搭建指南

    HarmonyOS开发环境搭建指南:DevEco Studio安装教程 一.系统要求 操作系统:Windows 10 64位或更高版本 RAM:至少8GB,推荐16GB 硬盘空间:至少10GB可用空间 ...

  3. 如何在 Epicor 中计算绩效

    制造性能是任何生产工序的关键,允许企业衡量和评估其效率和生产力水平. 我们将探讨如何在 Epicor 中计算制造性能.计算整体设备效率(OEE) 时性能指标的价值.如何解释制造指标以及在 Epicor ...

  4. openwrt交换机配置命令-swconfig

    swconfig swconfig 是交换接口 (switch) 配置命令. 交换机是二层设备,是我们用来配置vlan的必备利器. 使用swconfig list可以列出当前可用的 SWITCH 设备 ...

  5. OpenEuler文件被锁定的解决方法|网卡修改不生效的解决办法

    欧拉系统(含centos等linux系统)修改文件,一直提示readonly,不让改.原因有可能是这个文件给锁定了. 解决方法: 使用以下两个命令: • chattr 改变文件属性 • lsattr ...

  6. CentOS7.8安装k8s

    1, 安装 docker / kubelet # 在 master 节点和 worker 节点都要执行 \# 最后一个参数 1.20.6 用于指定 kubenetes 版本,支持所有 1.20.x 版 ...

  7. ChatGPT生成测试用例的最佳实践(三)

    还记得在第1章,我们利用ChatGPT生成的业务用例吗?这种业务用例生成方式其实和场景法用例设计十分相似,我们是不是也可以直接将业务用例输入ChatGPT,让它输出测试用例呢?笔者输入相关提示词让其补 ...

  8. 鸿蒙应用开发从入门到入行 - 篇4:层叠布局、自定义组件、ForEach

    第四篇 - 层叠布局.自定义组件.ForEach循环生成组件 导读:在本篇文章里,您将掌握层叠布局.自定义组件的用法,特别是自定义组件将来的开发中必然会用,其中应该特别关注自定义组件的一些规范与装饰器 ...

  9. Spring Security并结合JWT实现用户认证(Authentication) 和用户授权(Authorization)

    引言在Web应用开发中,安全一直是非常重要的一个方面.Spring Security基于Spring 框架,提供了一套Web应用安全性的完整解决方案. JwT (JSON Web Token) 是当前 ...

  10. 免费学习基于SpringBoot的高考志愿智能推荐系统

    免费学习基于SpringBoot的高考志愿智能推荐系统 摘要 科学技术日新月异,人们的生活都发生了翻天覆地的变化,高考志愿智能推荐系统管理当然也不例外.过去的信息管理都使用传统的方式实行,既花费了时间 ...