Apache Camel系列(4)----Akka Camel
<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> <dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-camel_2.11</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
/**
* Created by sam on 5/9/16.
*/
public class MyRedisProducer extends UntypedProducerActor { public void preStart() {
super.preStart();
} @Override
public String getEndpointUri() {
return "spring-redis://localhost:9999?connectionFactory=#connectionFactory&serializer=#serializer";
} @Override
public void onRouteResponse(Object message) {
System.out.println("response from route:{}" + message);
} @Override
public Object onTransformOutgoingMessage(Object message) {
if (message instanceof CamelMessage) {
CamelMessage camelMessage = (CamelMessage) message;
return camelMessage;
} else {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("CamelRedis.Command", "PUBLISH");
headers.put("CamelRedis.Channel", "testChannel");
headers.put("CamelRedis.Message", message.toString());
CamelMessage camelMessage = new CamelMessage(message, headers);
return camelMessage;
}
}
}
/**
* Created by sam on 5/9/16.
*/
public class MyRedisConsumer extends UntypedConsumerActor { @Override
public String getEndpointUri() {
return "spring-redis://localhost:9999?connectionFactory=#connectionFactory&serializer=#serializer&channels=testChannel&command=SUBSCRIBE";
} @Override
public void onReceive(Object o) throws Exception {
System.out.println(o);
if (o instanceof CamelMessage) {
CamelMessage msg = (CamelMessage) o;
System.out.println(msg.getBodyAs(String.class, getCamelContext()));
}
}
}
/**
* Created by sam on 5/9/16.
*/
public class RedisTest {
public static void main(String[] args) throws Exception {
ActorSystem system = ActorSystem.create("redis-actor");
Camel camel = CamelExtension.get(system); // 获取Camel对象,该对象可以直接操作Camel,比如获取CamelContext对象等。
PropertyPlaceholderDelegateRegistry delegateRegistry = (PropertyPlaceholderDelegateRegistry) camel.context().getRegistry();
JndiRegistry registry = (JndiRegistry) delegateRegistry.getRegistry(); // Apache Camel默认使用JndiRegistry来注册类信息。
if (registry.lookup("connectionFactory") == null && registry.lookup("serializer") == null) {
// 添加beans
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("localhost");
connectionFactory.setPassword("1234567890");
connectionFactory.setPort(9999);
// call this method to initialize connection factory
connectionFactory.afterPropertiesSet();
registry.bind("connectionFactory", connectionFactory); registry.bind("serializer", new StringRedisSerializer());
} // 创建producer和consumer
ActorRef producer = system.actorOf(Props.create(MyRedisProducer.class), "redisProducer");
ActorRef consumer = system.actorOf(Props.create(MyRedisConsumer.class), "redisConsumer"); while (true) {
Thread.sleep(1000);
producer.tell(new Date().toString(), ActorRef.noSender());
}
}
}
java.naming.factory.initial = org.apache.camel.util.jndi.CamelInitialContextFactory
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.
response from route:{}CamelMessage(null, Map())
CamelMessage(Thu May 12 09:38:04 CST 2016, Map(MessageExchangeId -> ID-localhost-localdomain-33258-1463017082575-0-2, breadcrumbId -> ID-localhost-localdomain-33258-1463017082575-0-1, CamelRedis.Channel -> testChannel, CamelRedis.Pattern -> [B@5910162e))
Thu May 12 09:38:04 CST 2016
response from route:{}CamelMessage(null, Map())
CamelMessage(Thu May 12 09:38:05 CST 2016, Map(MessageExchangeId -> ID-localhost-localdomain-33258-1463017082575-0-4, breadcrumbId -> ID-localhost-localdomain-33258-1463017082575-0-3, CamelRedis.Channel -> testChannel, CamelRedis.Pattern -> [B@4154c5ce))
Thu May 12 09:38:05 CST 2016
response from route:{}CamelMessage(null, Map())
CamelMessage(Thu May 12 09:38:06 CST 2016, Map(MessageExchangeId -> ID-localhost-localdomain-33258-1463017082575-0-6, breadcrumbId -> ID-localhost-localdomain-33258-1463017082575-0-5, CamelRedis.Channel -> testChannel, CamelRedis.Pattern -> [B@2cb03be0))
Thu May 12 09:38:06 CST 2016
Apache Camel系列(4)----Akka Camel的更多相关文章
- Apache Shiro系列之五,概述 —— 配置
Shiro设计的初衷就是可以运行于任何环境:无论是简单的命令行应用程序还是复杂的企业集群应用.由于运行环境的多样性,所以有多种配置机制可用于配置,本节我们将介绍Shiro内核支持的这几种配置机制. ...
- Apache Shiro系列四,概述 —— Shiro的架构
Shiro的设计目标就是让应用程序的安全管理更简单.更直观. 软件系统一般是基于用户故事来做设计.也就是我们会基于一个客户如何与这个软件系统交互来设计用户界面和服务接口.比如,你可能会说:“如 ...
- Apache Shiro系列三,概述 —— 10分钟入门
一.介绍 看完这个10分钟入门之后,你就知道如何在你的应用程序中引入和使用Shiro.以后你再在自己的应用程序中使用Shiro,也应该可以在10分钟内搞定. 二.概述 关于Shiro的废话就不多说了 ...
- apache kafka系列之Producer处理逻辑
最近研究producer的负载均衡策略,,,,我在librdkafka里边用代码实现了partition 值的轮询方法,,,但是在现场验证时,他的负载均衡不起作用,,,所以来找找原因: 下文是一篇描 ...
- Apache Shiro系列一,概述 —— 初识
一.什么是Shiro Apache Shiro是一个强大.灵活.开源的安全框架,它支持用户认证.权限控制.企业会话管理以及加密等. Apache Shiro的第一个也是最重要的一个目标就是易于使用和理 ...
- Apache Phoenix系列 | 从入门到精通(转载)
原文地址:https://cloud.tencent.com/developer/article/1498057 来源: 云栖社区 作者: 瑾谦 By 大数据技术与架构 文章简介:Phoenix是一个 ...
- Apache Shiro系列(1)
Apache Shiro是啥呢,安全框架. 360百科是这么描述的: Apache Shiro(日语"堡垒(Castle)"的意思)是一个强大易用的Java安全框架, ...
- Apache Shiro系列二,概述 —— 基本概念
做任何事情,首先要双方就一些概念的理解达成一致,这样大家就有共同语言,后续的沟通效率会高一些. #,Authentication,认证,也就是验证用户的身份,就是确定你是不是你,比如通过用户名.密码的 ...
- Apache Commons 系列简介 之 Pool
一.概述 Apache Commons Pool库提供了一整套用于实现对象池化的API,以及若干种各具特色的对象池实现.2.0版本,并非是对1.x的简单升级,而是一个完全重写的对象池的实现,显著的提升 ...
- Apache Shiro系列教程之二:十分钟上手Shiro
在本教程中,我们会写一个简单的.仅仅输出一些内容命令行程序,从而对Shiro有一个大体的感觉. 一.准备工作 本教程需要Java1.5+,并且我们用Maven生成项目,当然Maven不是必须的,你也可 ...
随机推荐
- golang模板库之fasttemplate
简介 fasttemplate是一个比较简单.易用的小型模板库.fasttemplate的作者valyala另外还开源了不少优秀的库,如大名鼎鼎的fasthttp,前面介绍的bytebufferpoo ...
- golang之循环导包
作为一个 Golang 开发,你可能在项目中遇到过包的循环依赖问题.Golang 不允许循环依赖,如果检测到代码中存在这种情况,在编译时就会抛出异常. 循环依赖 假设我们有两个包:p1和p2.当包p1 ...
- Python之读写Excel
现有的Excel分为两种格式:xls(Excel 97-2003)和xlsx(Excel 2007及以上). Python处理Excel文件主要是第三方模块库xlrd.xlwt.pyexcel-xls ...
- rabbitmq-c与amqp扩展安装
最近需要使用RabbitMQ进行消息队列处理 1.安装rabbitmq-c 在安装amqp之前需要先安装rabbitmq-c扩展 rabbitmq-c下载网址:https://github.com/a ...
- Nuxt.js 应用中的 beforeResponse 事件钩子
title: Nuxt.js 应用中的 beforeResponse 事件钩子 date: 2024/12/5 updated: 2024/12/5 author: cmdragon excerpt: ...
- 拥抱云原生,数据湖加速器 GooseFS 助力 Fluid 数据缓存实现
01 前言 数据湖加速器 GooseFS 是由腾讯云推出的高性能.高可用.弹性的分布式缓存方案.依靠对象存储(Cloud Object Storage,COS)作为数据湖存储底座的成本优势,为数据湖 ...
- Electron 窗体 BrowserWindow
http://jsrun.net/t/KfkKp https://www.wenjiangs.com/doc/tlsizw1dst https://www.w3cschool.cn/electronm ...
- spring 项目实现带请求链路id的日志记录
我们在做java项目的时候通常需要通过请求日志来排查定位线上问题,在日志比较多而我们又需要查找整个请求的全部日志的时候会比较困难.所以,就需要在日志记录的时候讲同一个请求的关键日志用同一个唯一标识串联 ...
- 【数据库】MySQL概念性基础知识期末复习
选择题 第一章 3 二维表结构--数据模型--关系数据模型 5 描述全部数据整体逻辑结构--模式 6 逻辑数据独立性--模式变,外模式和应用程序不变 7 物理数据独立性--内模式变,外模式和应用程序不 ...
- Qt编写地图综合应用26-覆盖物交互
一.前言 百度地图本身提供了非常友好完善的JS函数接口用于添加各种覆盖物,比如标注点.矩形区域.圆形区域.不规则线段.弧形等,基本上涵盖了各种应用场景,官方的文档和示例也是比较完善的,虽然示例用的都是 ...